|
| 1 | +package com.laigeoffer.pmhub.auth.config; |
| 2 | + |
| 3 | +import com.laigeoffer.pmhub.auth.handle.AuthenticationEntryPointImpl; |
| 4 | +import com.laigeoffer.pmhub.base.security.filter.JwtAuthenticationTokenFilter; |
| 5 | +import com.laigeoffer.pmhub.base.security.handle.LogoutSuccessHandlerImpl; |
| 6 | +import com.laigeoffer.pmhub.base.security.properties.PermitAllUrlProperties; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.context.annotation.Bean; |
| 9 | +import org.springframework.http.HttpMethod; |
| 10 | +import org.springframework.security.authentication.AuthenticationManager; |
| 11 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 12 | +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; |
| 13 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 14 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 15 | +import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer; |
| 16 | +import org.springframework.security.config.http.SessionCreationPolicy; |
| 17 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 18 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 19 | +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
| 20 | +import org.springframework.security.web.authentication.logout.LogoutFilter; |
| 21 | +import org.springframework.web.filter.CorsFilter; |
| 22 | + |
| 23 | +/** |
| 24 | + * spring security配置 |
| 25 | + * |
| 26 | + * @author canghe |
| 27 | + */ |
| 28 | +@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) |
| 29 | +public class SecurityConfig extends WebSecurityConfigurerAdapter { |
| 30 | + /** |
| 31 | + * 自定义用户认证逻辑 |
| 32 | + */ |
| 33 | + @Autowired |
| 34 | + private UserDetailsService userDetailsService; |
| 35 | + |
| 36 | + /** |
| 37 | + * 认证失败处理类 |
| 38 | + */ |
| 39 | + @Autowired |
| 40 | + private AuthenticationEntryPointImpl unauthorizedHandler; |
| 41 | + |
| 42 | + /** |
| 43 | + * 退出处理类 |
| 44 | + */ |
| 45 | + @Autowired |
| 46 | + private LogoutSuccessHandlerImpl logoutSuccessHandler; |
| 47 | + |
| 48 | + /** |
| 49 | + * token认证过滤器 |
| 50 | + */ |
| 51 | + @Autowired |
| 52 | + private JwtAuthenticationTokenFilter authenticationTokenFilter; |
| 53 | + |
| 54 | + /** |
| 55 | + * 跨域过滤器 |
| 56 | + */ |
| 57 | + @Autowired |
| 58 | + private CorsFilter corsFilter; |
| 59 | + |
| 60 | + /** |
| 61 | + * 允许匿名访问的地址 |
| 62 | + */ |
| 63 | + @Autowired |
| 64 | + private PermitAllUrlProperties permitAllUrl; |
| 65 | + |
| 66 | + /** |
| 67 | + * 解决 无法直接注入 AuthenticationManager |
| 68 | + * |
| 69 | + * @return |
| 70 | + * @throws Exception |
| 71 | + */ |
| 72 | + @Bean |
| 73 | + @Override |
| 74 | + public AuthenticationManager authenticationManagerBean() throws Exception { |
| 75 | + return super.authenticationManagerBean(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * anyRequest | 匹配所有请求路径 |
| 80 | + * access | SpringEl表达式结果为true时可以访问 |
| 81 | + * anonymous | 匿名可以访问 |
| 82 | + * denyAll | 用户不能访问 |
| 83 | + * fullyAuthenticated | 用户完全认证可以访问(非remember-me下自动登录) |
| 84 | + * hasAnyAuthority | 如果有参数,参数表示权限,则其中任何一个权限可以访问 |
| 85 | + * hasAnyRole | 如果有参数,参数表示角色,则其中任何一个角色可以访问 |
| 86 | + * hasAuthority | 如果有参数,参数表示权限,则其权限可以访问 |
| 87 | + * hasIpAddress | 如果有参数,参数表示IP地址,如果用户IP和参数匹配,则可以访问 |
| 88 | + * hasRole | 如果有参数,参数表示角色,则其角色可以访问 |
| 89 | + * permitAll | 用户可以任意访问 |
| 90 | + * rememberMe | 允许通过remember-me登录的用户访问 |
| 91 | + * authenticated | 用户登录后可访问 |
| 92 | + */ |
| 93 | + @Override |
| 94 | + protected void configure(HttpSecurity httpSecurity) throws Exception { |
| 95 | + // 注解标记允许匿名访问的url |
| 96 | + ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity.authorizeRequests(); |
| 97 | + permitAllUrl.getUrls().forEach(url -> registry.antMatchers(url).permitAll()); |
| 98 | + |
| 99 | + httpSecurity |
| 100 | + // CSRF禁用,因为不使用session |
| 101 | + .csrf().disable() |
| 102 | + // 禁用HTTP响应标头 |
| 103 | + .headers().cacheControl().disable().and() |
| 104 | + // 认证失败处理类 |
| 105 | + .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() |
| 106 | + // 基于token,所以不需要session |
| 107 | + .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() |
| 108 | + // 过滤请求 |
| 109 | + .authorizeRequests() |
| 110 | + // 对于外部oa调用 单点登录 登录login 注册register 验证码captchaImage 允许匿名访问 |
| 111 | + .antMatchers("WW_verify_demRgYMCtoo6EQlR.txt","/WW_verify_BewKb7YjhLQkBDPD.txt","/oa/**","/sso/wx/**","/sso/oauth2/accessToken","/sso/oauth2/user", "/sso/oa/**","/login", "/register", "/captchaImage", "/websocket/**").permitAll() |
| 112 | + // 静态资源,可匿名访问 |
| 113 | + .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll() |
| 114 | + .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() |
| 115 | + // 除上面外的所有请求全部需要鉴权认证 |
| 116 | + .anyRequest().authenticated() |
| 117 | + .and() |
| 118 | + .headers().frameOptions().disable(); |
| 119 | + // 添加Logout filter |
| 120 | + httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler); |
| 121 | + // 添加JWT filter |
| 122 | + httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); |
| 123 | + // 添加CORS filter |
| 124 | + httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class); |
| 125 | + httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * 强散列哈希加密实现 |
| 130 | + */ |
| 131 | + @Bean |
| 132 | + public BCryptPasswordEncoder bCryptPasswordEncoder() { |
| 133 | + return new BCryptPasswordEncoder(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * 身份认证接口 |
| 138 | + */ |
| 139 | + @Override |
| 140 | + protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 141 | + auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder()); |
| 142 | + } |
| 143 | +} |
0 commit comments