2017-11-29 3 views
1

私は春のセキュリティを使用しています。ログインパスに/ api接頭辞を追加する必要があります。私が何とか「/ログイン」を上書きする必要がスプリングウェブとセキュリティ。ログインルートに/ api接頭辞を追加します

public class UsernamePasswordAuthenticationFilter extends 
    AbstractAuthenticationProcessingFilter { 
// ~ Static fields/initializers 
// ===================================================================================== 

public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "username"; 
public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "password"; 

private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY; 
private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY; 
private boolean postOnly = true; 

// ~ Constructors 
// =================================================================================================== 

public UsernamePasswordAuthenticationFilter() { 
    super(new AntPathRequestMatcher("/login", "POST")); 
} 

... 

:私は、ログインルートのコンストラクタのインスタンスで

JWTAuthenticationFiler.class

public class JWTAuthenticationFilter extends 
UsernamePasswordAuthenticationFilter{ 
private AuthenticationManager authenticationManager; 

public JWTAuthenticationFilter(AuthenticationManager authenticationManager) { 
    this.authenticationManager = authenticationManager; 
} 

@Override 
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) 
     throws AuthenticationException { 
    try { 
     User creds = new ObjectMapper() 
       .readValue(request.getInputStream(), User.class); 

     return authenticationManager.authenticate(
       new UsernamePasswordAuthenticationToken(
         creds.getUsername(), 
         creds.getPassword(), 
         new ArrayList<>()) 
     ); 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 
} 

@Override 
protected void successfulAuthentication(HttpServletRequest request, 
             HttpServletResponse response, 
             FilterChain chain, 
             Authentication authResult) throws IOException, ServletException { 
    String token = Jwts.builder() 
      .setSubject(((org.springframework.security.core.userdetails.User) authResult.getPrincipal()).getUsername()) 
      .setExpiration(new Date(System.currentTimeMillis() + SecurityConstants.EXPIRATION_TIME)) 
      .signWith(SignatureAlgorithm.HS512, SecurityConstants.SECRET.getBytes()) 
      .compact(); 
    response.addHeader(SecurityConstants.HEADER_STRING, token); 
} 

} 

基底クラスが含まれているUsernamePasswordAuthenticationFilterクラスを拡張しています"/ api/login"。どうやってするか?

ありがとうございました!

答えて

2

AbstractAuthenticationProcessingFilter見、あなたがsetRequiresAuthenticationRequestMatcherを使用する必要がUsernamePasswordAuthenticationFilterを拡張し、カスタム認証フィルタでfilterProcessesUrlのデフォルト値を変更するには:

このフィルタは、要求をインターセプトし、その要求から認証を実行しようとします要求がsetRequiresAuthenticationRequestMatcher(RequestMatcher)と一致する場合。

RequestMatcherは、あなたのカスタムURLと一致します。

コンストラクタ、ファクトリメソッドまたはXML設定でセッターを呼び出すことができます。

2

WebSecurityConfigurerAdapterを使用するとします。したがって、ログインエンドポイントを設定するには、formLoginを使用する必要があります。

public class SecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 

    protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests() 
     .antMatchers("/apilogin*").permitAll() 
     .anyRequest().authenticated() 
     .and().formLogin().loginPage("/apilogin") 
     .and().httpBasic(); 
    } 
} 
+0

ご回答いただきありがとうございますが、私の質問は悪いとされていました。もう一度質問を確認できますか? –

+0

'.antMatchers("/apilogin * ")。permitAll()'は必要ありません。さらに、マッチングは広すぎる可能性があります。 – dur

1

私はあなたの設定クラスにBeanを定義する際にフィルタにsetRequiresAuthenticationRequestMatcherを呼び出す必要があります信じています。

code.setRequiresAuthenticationRequestMatcher(
    new AntPathRequestMatcher("/api/login","POST")); 

@Bean 
public UsernamePasswordAuthenticationFilter authenticationFilter() { 
    UsernamePasswordAuthenticationFilter authFilter = new UsernamePasswordAuthenticationFilter(); 
    authFilter.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login","POST")); 

    return authFilter; 
} 
関連する問題