Blame view

src/main/java/com/objecteye/config/WebSecurityConfig.java 3.34 KB
c83b5b39   Liu Haoyu   项目创建, 集成spring-se...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  package com.objecteye.config;
  
  import com.objecteye.common.ResultCode;
  import com.objecteye.service.impl.UserDetailServiceImpl;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.config.http.SessionCreationPolicy;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.security.jwt.crypto.sign.RsaSigner;
  import org.springframework.security.jwt.crypto.sign.RsaVerifier;
  import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  
  @Configuration
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private RsaVerifier verifier;
  
      @Autowired
      private RsaSigner signer;
  
      @Autowired
      private UserDetailServiceImpl userDetailService;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          AuthenticationLoginFilter authenticationLoginFilter = new AuthenticationLoginFilter();
          authenticationLoginFilter.setAuthenticationManager(this.authenticationManagerBean());
  
          LoginSuccessHandler loginSuccessHandler = new LoginSuccessHandler();
          loginSuccessHandler.setSigner(signer);
          authenticationLoginFilter.setAuthenticationSuccessHandler(loginSuccessHandler);
          authenticationLoginFilter.setAuthenticationFailureHandler(new LoginFailureHandler());
  
          AuthenticationProviderConfig provider = new AuthenticationProviderConfig();
          provider.setPasswordEncoder(passwordEncoder());
          provider.setUserDetailsService(userDetailService);
  
          AuthenticationHeadFilter headFilter = new AuthenticationHeadFilter();
          headFilter.setRsaVerifier(verifier);
  
          http.exceptionHandling().authenticationEntryPoint((request, response, authException) -> {
              response.setContentType("application/json;charset=UTF-8");
              response.getWriter().write(ResultCode.UNAUTHORIZED.getMessage());
          })
                  .accessDeniedHandler((httpServletRequest, httpServletResponse, accessDeniedException) -> {
                      httpServletResponse.setContentType("application/json;charset=UTF-8");
                      httpServletResponse.getWriter().write(ResultCode.FORBIDDEN.getMessage());
                  })
                  .and()
                  .authorizeRequests()
                  .anyRequest().access("@accessConfirmService.hasPermission(request , authentication)")
                  .and()
                  //注册授权管理器(AuthenticationManager)
                  .authenticationProvider(provider)
                  .addFilterAfter(authenticationLoginFilter, UsernamePasswordAuthenticationFilter.class)
                  .addFilterAfter(headFilter, AuthenticationLoginFilter.class)
                  //禁用session
                  .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                  .and()
                  .csrf().disable();
      }
  
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
  }