在常用的后台管理系统中 , 通常都会有访问权限控制的需求,用于限制不同人员对于接口的访问能力 , 如果用户不具备指定的权限,则不能访问某些接口 。
本文将用 waynboot-mall 项目举例,给大家介绍常见后管系统如何引入权限控制框架 Spring Security 。大纲如下:
![Spring Security权限控制框架使用指南](http://img.jiangsulong.com/240219/16453Q300-0.jpg)
文章插图
waynboot-mall 项目地址:https://Github.com/wayn111/waynboot-mall
一、什么是 Spring SecuritySpring Security 是一个基于 Spring 框架的开源项目,旨在为 JAVA 应用程序提供强大和灵活的安全性解决方案 。Spring Security 提供了以下特性:
- 认证:支持多种认证机制,如表单登录、HTTP 基本认证、OAuth2、OpenID 等 。
- 授权:支持基于角色或权限的访问控制 , 以及基于表达式的细粒度控制 。
- 防护:提供了多种防护措施 , 如防止会话固定、点击劫持、跨站请求伪造等攻击 。
- 集成:与 Spring 框架和其他第三方库和框架进行无缝集成,如 Spring MVC、Thymeleaf、Hibernate 等 。
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId><version>3.1.0</version></dependency></dependencies>
三、如何配置 Spring Security在 Spring Security 3.0 中要配置 Spring Security 跟以往是有些不同的,比如不在继承 WebSecurityConfigurerAdapter 。在 waynboot-mall 项目中 , 具体配置如下,@Configuration@EnableWebSecurity@AllArgsConstructor@EnableMethodSecurity(securedEnabled = true, jsr250Enabled = true)public class SecurityConfig {private UserDetAIlsServiceImpl userDetailsService;private AuthenticationEntryPointImpl unauthorizedHandler;private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;private LogoutSuccessHandlerImpl logoutSuccessHandler;@Beanpublic SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {httpSecurity// cors启用.cors(httpSecurityCorsConfigurer -> {}).csrf(AbstractHttpConfigurer::disable).sessionManagement(httpSecuritySessionManagementConfigurer -> {httpSecuritySessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS);}).exceptionHandling(httpSecurityExceptionHandlingConfigurer -> {httpSecurityExceptionHandlingConfigurer.authenticationEntryPoint(unauthorizedHandler);})// 过滤请求.authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> {authorizationManagerRequestMatcherRegistry.requestMatchers("/favicon.ico", "/login", "/favicon.ico", "/actuator/**").anonymous().requestMatchers("/slider/**").anonymous().requestMatchers("/captcha/**").anonymous().requestMatchers("/upload/**").anonymous().requestMatchers("/common/download**").anonymous().requestMatchers("/doc.html").anonymous().requestMatchers("/swagger-ui/**").anonymous().requestMatchers("/swagger-resources/**").anonymous().requestMatchers("/webjars/**").anonymous().requestMatchers("/*/api-docs").anonymous().requestMatchers("/druid/**").anonymous().requestMatchers("/elastic/**").anonymous().requestMatchers("/message/**").anonymous().requestMatchers("/ws/**").anonymous()// 除上面外的所有请求全部需要鉴权认证.anyRequest().authenticated();}).headers(httpSecurityHeadersConfigurer -> {httpSecurityHeadersConfigurer.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable);});// 处理跨域请求中的Preflight请求(cors),设置corsConfigurationSource后无需使用// .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()// 对于登录login 验证码captchaImage 允许匿名访问httpSecurity.logout(httpSecurityLogoutConfigurer -> {httpSecurityLogoutConfigurer.logoutUrl("/logout");httpSecurityLogoutConfigurer.logoutSuccessHandler(logoutSuccessHandler);});// 添加JWT filterhttpSecurity.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);// 认证用户时用户信息加载配置 , 注入springAuthUserServicehttpSecurity.userDetailsService(userDetailsService);return httpSecurity.build();}@Beanpublic AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {return authenticationConfiguration.getAuthenticationManager();}/*** 强散列哈希加密实现*/@Beanpublic BCryptPasswordEncoder bCryptPasswordEncoder() {return new BCryptPasswordEncoder();}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 华为root权限怎么开启 华为root权限怎么开启方法
- 如何获取termux最高权限,Termux提示没有权限怎么办
- 朋友圈权限里标签如何删除 朋友圈设置标签怎么删除
- 如何打开jsp文件 如何打开jsp文件权限
- 微信怎么设置对所有人关闭朋友圈权限 微信怎么设置对所有人关闭朋友圈
- 王者荣耀查阅权限怎么设置
- vivo手机root权限怎么开启教程 vivo手机root权限怎么开启
- qq怎么设置应用权限,手机QQ上怎么设置说说评论权限
- 知乎怎么申请直播权限呢 知乎怎么申请直播权限
- 微视怎么开通流量托管权限