Spring Security权限控制框架使用指南
Spring
为网友们解答Spring的相关经验,接下来IT袋网带大家一起了解。
在常用的后台管理系统中,通常都会有访问权限控制的需求,用于限制不同人员对于接口的访问能力,如果用户不具备指定的权限,则不能访问某些接口。
本文将用waynboot-mall项目举例,给大家介绍常见后管系统如何引入权限控制框架Spring Security。大纲如下,

waynboot-mall 项目地址:https://github.com/wayn111/waynboot-mall
一、什么是Spring Security
Spring Security 是一个基于 Spring 框架的开源项目,旨在为 Java 应用程序提供强大和灵活的安全性解决方案。Spring Security 提供了以下特性:
- 认证:支持多种认证机制,如表单登录、HTTP 基本认证、OAuth2、OpenID 等。
- 授权:支持基于角色或权限的访问控制,以及基于表达式的细粒度控制。
- 防护:提供了多种防护措施,如防止会话固定、点击劫持、跨站请求伪造等攻击。
- 集成:与 Spring 框架和其他第三方库和框架进行无缝集成,如 Spring MVC、Thymeleaf、Hibernate 等。
二、如何引入Spring Security
在 waynboot-mall 项目中直接引入 spring-boot-starter-security 依赖,
<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;
@Bean
public 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 filter
httpSecurity.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
// 认证用户时用户信息加载配置,注入springAuthUserService
httpSecurity.userDetailsService(userDetailsService);
return httpSecurity.build();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
/**
* 强散列哈希加密实现
*/
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
相关阅读
-
网站制作好公司有哪些 网站建设企业建站
一篇方法教程,与您分享网站制作好公司有哪些和网站建设企业建站IT技巧方面的经验,一起来看看吧! 成为一家优秀的网站定制设计公司不容易,选择一家合适自己的高端网站定制设计公司
-
数据库系统有哪些特点 简单数据库设计案例
小编为你介绍数据库系统有哪些特点和简单数据库设计案例方面的讲解,一起来看看吧! 数据库系统概论(第5版)第1版于1983年出版,至今已修订至第5版。《数据库系统概论(第5版)》系统
-
中间件解析:概念、类型与实际应用全面探讨
为网友们详解中间件解析的教程内容,一起来看看吧! 一、什么是中间件 在软件开发领域,中间件(Middleware)是一种位于操作系统和应用程序之间的软件,它为应用程序提供了一种统一的、
-
深入解析:“Access Denied”错误的含义及处理方法
相对于大多数人及“Access的相关介绍,请看下面详细的介绍。 在我们使用计算机或网络服务时,可能会遇到Access Denied的错误信息。 这个信息对于新手来说可能会让人感到困惑,那么它到底是


