New file |
| | |
| | | package com.panzhihua.zuul.config; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.security.config.annotation.ObjectPostProcessor; |
| | | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| | | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| | | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| | | import org.springframework.security.config.http.SessionCreationPolicy; |
| | | import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; |
| | | import org.springframework.security.web.authentication.AnonymousAuthenticationFilter; |
| | | |
| | | import com.panzhihua.zuul.filters.JWTAuthenticationTokenFilter; |
| | | import com.panzhihua.zuul.filters.SercuritFilter; |
| | | import com.panzhihua.zuul.handles.UserAuthAccessDeniedHandler; |
| | | import com.panzhihua.zuul.manager.RoleAccessDecisionManager; |
| | | |
| | | /** |
| | | * @program: springcloud_k8s_panzhihuazhihuishequ |
| | | * @description: 安全 |
| | | * @author: huang.hongfa weixin hhf9596 qq 959656820 |
| | | * @create: 2020-11-25 10:57 |
| | | **/ |
| | | @Configuration |
| | | @EnableWebSecurity |
| | | public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { |
| | | |
| | | @Resource |
| | | private SercuritFilter filter; |
| | | @Resource |
| | | private RoleAccessDecisionManager roleAccessDecisionManager; |
| | | /** |
| | | * 自定义暂无权限处理器 |
| | | */ |
| | | @Resource |
| | | private UserAuthAccessDeniedHandler userAuthAccessDeniedHandler; |
| | | |
| | | @Override |
| | | protected void configure(HttpSecurity http) throws Exception { |
| | | http.authorizeRequests().withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() { |
| | | @Override |
| | | public <O extends FilterSecurityInterceptor> O postProcess(O o) { |
| | | o.setAccessDecisionManager(roleAccessDecisionManager); |
| | | o.setSecurityMetadataSource(filter); |
| | | return o; |
| | | } |
| | | }).anyRequest().authenticated().and() |
| | | // 配置没有权限自定义处理类 |
| | | .exceptionHandling().accessDeniedHandler(userAuthAccessDeniedHandler).and().csrf().disable(); |
| | | // 基于Token不需要session |
| | | http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); |
| | | // 禁用缓存 |
| | | http.headers().cacheControl(); |
| | | http.addFilterBefore(new JWTAuthenticationTokenFilter(), AnonymousAuthenticationFilter.class); |
| | | } |
| | | |
| | | } |