2017-05-31 25 views
-1

我不明白這段代碼是如何工作的,尤其是OncePerRequestFilter類。這個類的目的是什麼,我已經把代碼粘貼給我了。如何在春季啓動時驗證用戶?

public class AuthenticationFilter extends OncePerRequestFilter{ 

private final LoginService loginService; 

private static final Logger logger = Logger.getLogger(AuthenticationFilter.class); 

public AuthenticationFilter(final LoginService loginService) { 
    super(); 
    this.loginService = loginService; 
} 

@Override 
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) 
     throws ServletException, IOException { 
    final String xAuth = request.getHeader("X-Authorization");  
+0

見春季啓動的例子:https://spring.io/guides/gs/securing-web/ – Sudhakar

+0

我想刪除這個問題,你可以告訴我如何刪除它。 –

回答

0

認證和授權是兩個不同的術語。 1.身份驗證:您是您所聲稱的人。 2.授權:你可以做什麼。

假設:您的問題是授權:「我想授權基於rest api的特定用戶」。

配置http.authorizeRequests()antMatchers( 「/產品」)訪問( 「hasRole( 'ROLE_ADMIN')」)

protected void configure(HttpSecurity http) throws Exception { 

    http.authorizeRequests().antMatchers("/products").access("hasRole('ROLE_ADMIN')").anyRequest().permitAll().and().authorizeRequests().antMatchers("/hello").access("hasRole('ROLE_ADMIN')").anyRequest().permitAll().and() 
      .formLogin().loginPage("/login").usernameParameter("username").passwordParameter("password").and() 
      .logout().logoutSuccessUrl("/login?logout").and().exceptionHandling().accessDeniedPage("/403").and() 
      .csrf(); 
} 

是指用於全碼:。。https://github.com/Roshanmutha/SpringSecurityJDBC/blob/master/src/main/java/com/roshantest/WebSecurityConfig.java