2017-07-07 8 views
2

私はSpringブートアプリケーションで両方のセキュリティを使用することを目指しています。私は既にJWTでAPI側を行っていますが、WEB側のセッションを実装する方法はわかりません。私はすでに別のプロジェクトでそれを行っていますが、一緒に働かせる方法はわかりません。ここでSpring Security:API用のJWTトークンとWeb用のセッション

は私SecurityConfigです:

@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(); 
} 

誰もがそれを行う方法を教えてもらえます:

@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)); 
} 

私はこのような何かを持っていると思いますか? (そしてそれがどのように働くか私に説明する)。 私は何を求めているのか良い解決策を見つけられませんでした。検索の6時間後

答えて

2

は、ここでの解決策は次のとおりです。 https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#multiple-httpsecurity

EDIT:これは助けることができる

@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(); 
     } 
    } 
} 

希望: ここに私がやったことをどのようにです!

関連する問題