2016-04-09 16 views
3

基本的な認証済みのRESTアプリケーションが必要です。私はhttp://www.baeldung.com/spring-security-authentication-providerからの一般的な指示に従ってセキュリティを働かせました。カスタム認証プロバイダが呼び出されていません

私はAuthenticationProviderという実装を作成しましたが、Springによって呼び出されることはありません。すべてのリクエストには、AuthenticationProviderが何もしていないのに、エラー:

{"timestamp":1460199213227,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/test"} 

がなくなります。

アプリは、アノテーションベースであり、ここでは関係ビットです:

セキュリティ設定

@Configuration 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
public class ApplicationSecurity extends WebSecurityConfigurerAdapter { 
    @Autowired 
    CustomAuthenticationProvider authenticationProvider; 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(authenticationProvider); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
       .csrf().disable() 
       .authenticationProvider(authenticationProvider) 
       .authorizeRequests() 
       .anyRequest().authenticated().and().httpBasic(); 
    } 
} 

AuthenticationProvider

@Component 
public class CustomAuthenticationProvider implements AuthenticationProvider { 
    @Autowired 
    private UserDAO userDAO; 
    @Autowired 
    private Authenticator authenticator; 

    @Override 
    public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
     // This never gets called, I checked with debugger 
     String username = authentication.getName(); 
     String password = authentication.getCredentials().toString(); 

     User user = userDAO.findByUsername(username); 
     User authenticatedUser = authenticator.authenticate(user, password); 
     if (authenticatedUser == null){ 
      throw new RESTAuthenticationException("Auth failed"); 
     } 

     List<GrantedAuthority> authorityList = new ArrayList<>(); 
     return new UsernamePasswordAuthenticationToken(user, authorityList); 
    } 

    @Override 
    public boolean supports(Class<?> aClass) { 
     return aClass.equals(UsernamePasswordAuthenticationToken.class); 
    } 
} 

コントローラ

@RestController 
public class UserController { 
    @RequestMapping(value = "/test") 
    public ResponseEntity test(@AuthenticationPrincipal User user) { 
     return ResponseEntity.ok().body(user); 
    } 
} 
+0

@Configurationクラスでは、DeadlinesAuthenticationProviderを使用しますが、CustomAuthenticationProviderのコードを投稿します。それはあなたの問題ではありませんか? – ben75

+0

投稿時にコードを手作業で編集していたので、私の部分ではエラーに過ぎず、コードは正しく設定されています。固定 –

+0

OK。あなたは/ testをどう呼びますか?カールのようなツールを使用していますか、ブラウザなどを使用していますか? – ben75

答えて

3

ステータスコード401の応答を受け取ります。これは"unauthorized" http status codeです。これはおそらく、あなたの要求の中に、間違った/不正な形式の認証ヘッダーが原因です。

あなたがHttp-Basicを使用している:文字列がQWxhZGRpbjpPcGVuU2VzYW1l<user>:<password> BASE64でエンコードされた文字列である

Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l 

:その要求に次のヘッダーを必要とします。

+0

私は気になるでしょう。私が送っていたヘッダーは 'Authorization:Basic:'でした。余分なコロンが問題でした。 –

関連する問題