2017-07-07 98 views
2

我的目標是在我的Spring Boot應用程序中使用這兩種安全性。我已經使用JWT完成了API方面的工作,但是我不知道如何實現WEB端的會話。我已經在另一個項目中這樣做了,但我不知道如何讓它們一起工作。Spring Security:適用於API的API和會話的JWT令牌

這裏是我的SecurityConfig

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.csrf().ignoringAntMatchers("/api/**") 
     .and() 
      .sessionManagement() 
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
     .and() 
      .authorizeRequests() 
      .antMatchers("/api/register").permitAll() 
      .antMatchers("/api/login").permitAll() 
      .antMatchers("/api/public").permitAll() 
      .antMatchers("/api/lost").permitAll() 
      .antMatchers("/").permitAll() 
      .antMatchers("/login").permitAll() 
      .antMatchers("/contact").permitAll() 
      .antMatchers("/resources/**").permitAll() 
      .antMatchers("/file/**").permitAll() 
      .anyRequest().authenticated() 
     .and() 
      .apply(new JWTConfigurer(this.tokenProvider)); 
} 

我想有這樣的事情:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     // For API side something like : .match("/api/**") 
     // No CSRF 
     .csrf().ignoringAntMatchers("/api/**") 
     // STATELESS session 
     // Use token filter 
     .apply(new JWTConfigurer(this.tokenProvider)); 

     // For WEB side something like : .match "others" 
     // Use CSRF 
     .csrf() 
     // Use session 

     // And the other permit : 
      .authorizeRequests() 
      .antMatchers("/api/register").permitAll() 
      .antMatchers("/api/login").permitAll() 
      .antMatchers("/api/public").permitAll() 
      .antMatchers("/api/lost").permitAll() 
      .antMatchers("/").permitAll() 
      .antMatchers("/login").permitAll() 
      .antMatchers("/contact").permitAll() 
      .antMatchers("/resources/**").permitAll() 
      .antMatchers("/file/**").permitAll() 
      .anyRequest().authenticated(); 
} 

誰能告訴我該怎麼做? (並解釋我是如何工作的)。 我還沒有找到任何好的解決方案,我問。

回答

2

6小時搜索後,這裏是解決方案: https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#multiple-httpsecurity

編輯: 這裏是我是怎麼做的:

@EnableWebSecurity 
public class MultiHttpSecurityConfig { 

    @Autowired 
    private UserDetailsService userDetailsService; 

    @Bean 
    public PasswordEncoder passwordEncoder() { 
     return new BCryptPasswordEncoder(12); 
    } 

    @Configuration 
    @Order(1) 
    public class ApiSecurityAdapter extends WebSecurityConfigurerAdapter { 

     private TokenProvider tokenProvider; 

     public ApiSecurityAdapter(TokenProvider tokenProvider) { 
      this.tokenProvider = tokenProvider; 
     } 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http.antMatcher("/api/**") //<= Security only available for /api/** 
       .authorizeRequests() 
        .antMatchers("/api/register").permitAll() 
        .antMatchers("/api/login").permitAll() 
        .antMatchers("/api/public").permitAll() 
        .antMatchers("/api/lost").permitAll() 
        .anyRequest().authenticated() 
       .and() 
        .apply(new JWTConfigurer(this.tokenProvider)) 
       .and() 
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 
     } 
    } 

    @Configuration 
    public class WebSecurityAdapter extends WebSecurityConfigurerAdapter { 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http // <= Security available for others (not /api/) 
       .authorizeRequests() 
        .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") 
        .antMatchers("/").permitAll() 
        .antMatchers("/login").permitAll() 
        .antMatchers("/resources/**").permitAll() 
        .anyRequest().authenticated() 
       .and() 
        .formLogin() 
         .loginPage("/login") 
          .usernameParameter("email") 
          .passwordParameter("password") 
          .defaultSuccessUrl("/central", false) 
          .failureForwardUrl("/login/fail") 
       .and() 
        .logout() 
         .invalidateHttpSession(true) 
         .logoutUrl("/logout") 
         .logoutSuccessUrl("/") 
       .and() 
        .csrf(); 
     } 
    } 
} 

希望這可以幫助!

相關問題