私自身のカスタム認証プロバイダを作成しています。今私はちょうど静的なユーザー名とパスワードを確認しているが、後でこれはより高度なものに置き換えられますので、私はこのケースでカスタムプロバイダを使用する必要はありませんが、追加コードはまだ追加されていません。スプリングブートセキュリティがカスタムAuthenticationProviderでロールを尊重しない
これはこれが壊れた状態の私のコードだと言いました。
マイカスタム認証プロバイダ:今私は、ユーザー名とパスワードなしでエンドポイントをヒットするカールを使用している場合、私はこれを実行すると
import com.comcast.iot.das.auth.SatAuthenticationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class DeviceSecurityConfig extends WebSecurityConfigurerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(SatAuthenticationProvider.class);
@Autowired
private SatAuthenticationProvider satAuthenticationProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
LOGGER.info("*** configuring http");
http.authorizeRequests().anyRequest()
.hasRole("USER")
.and()
.httpBasic();
}
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
LOGGER.info("*** Setting builder");
auth.authenticationProvider(this.satAuthenticationProvider);
}
}
:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class SatAuthenticationProvider implements AuthenticationProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(SatAuthenticationProvider.class);
public SatAuthenticationProvider() {
LOGGER.info("*** CustomAuthenticationProvider created");
}
@Override
public boolean supports(Class<? extends Object> authentication) {
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
LOGGER.info("*** Creating authentication");
if (authentication.getName().equals("test") && authentication.getCredentials().equals("test")) {
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("USER"));
grantedAuths.add(new SimpleGrantedAuthority("ADMIN"));
return new UsernamePasswordAuthenticationToken(authentication.getName(), authentication.getCredentials(), grantedAuths);
} else {
return null;
}
}
}
ここではされて消費し、私のセキュリティの設定です
% curl http://localhost:8080/molecule
{
"timestamp" : 1505925047977,
"status" : 401,
"error" : "Unauthorized",
"message" : "Full authentication is required to access this resource",
"path" : "/molecule"
}
正しいユーザー名とパスを指定すると私はそれを得ることができ、私が直接操作する役割を得るカントながら、
% curl -u test:test2 http://localhost:8080/molecule
{
"timestamp" : 1505925199406,
"status" : 401,
"error" : "Unauthorized",
"message" : "No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken",
"path" : "/molecule"
}
ワン最後のポイント:私は、次を取得し、間違ったユーザー名とパスワードを指定した場合、最後に
% curl -u test:test http://localhost:8080/molecule
{
"timestamp" : 1505925033015,
"status" : 403,
"error" : "Forbidden",
"message" : "Access is denied",
"path" : "/molecule"
}
:言葉私は次の取得しますユーザーがまったく認証されているかどうかをテストし、それを使用して許可を与えます。私は役割が必要なので実行可能な解決策ではありませんが、誰かがこの質問にいくつかのヒントに答えようとするかもしれません。
次のようにだから私はDeviceSecurityConfigクラスのconfigureメソッドを変更することができます。
@Override
protected void configure(HttpSecurity http) throws Exception {
LOGGER.info("*** configuring http");
http.authorizeRequests().anyRequest()
.authenticated()
.and()
.httpBasic();
}
コードのこの新しいバージョンでは期待通りの役割を追加する方法はありませんけれども、私のカール要求は(少なくとも仕事に思えるんもちろん):。ここで私が言及したコード編集のカール結果があります。
んが、ユーザー名とパスワード:
は% curl http://localhost:8080/molecule
{
"timestamp" : 1505925444957,
"status" : 401,
"error" : "Unauthorized",
"message" : "Full authentication is required to access this resource",
"path" : "/molecule"
}
誤ったパスワード:フル応答は、私は通常、期待のエンドポイントを形成返し今、次のコマンドを使用して
% curl -u test:test2 http://localhost:8080/molecule
{
"timestamp" : 1505925456018,
"status" : 401,
"error" : "Unauthorized",
"message" : "No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken",
"path" : "/molecule"
}
正しいパスワードとユーザー名(省略しません)。
% curl -u test:test http://localhost:8080/molecule