一文带你掌握Spring Security框架的使用
作者:蜀山剑客李沐白 发布时间:2021-12-02 09:41:00
Spring Security是一款基于Spring框架的认证和授权框架,提供了一系列控制访问和保护应用程序的功能,同时也支持基于角色和权限的访问控制,加密密码,CSRF防范,会话管理等多种功能。Spring Security可以轻松地与其他Spring框架,如Spring Boot和Spring MVC进行集成使用。
本文将会对Spring Security框架进行全面详细的讲解,包括框架的概述、认证、授权、LDAP身份验证、Kerberos身份验证、CSRF防范、加密密码、会话管理、异常处理等方面,并提供相关API。
框架概述
Spring Security是一个基于Spring框架的认证和授权框架,它提供了各种工具和框架来保护基于Spring的应用程序。Spring Security可以让开发人员和系统管理员轻松地配置各种安全功能,例如:
用户认证和授权
保护Web应用免受各种攻击,如跨站点脚本攻击(XSS)、跨站点请求伪造攻击(CSRF)和点击劫持攻击
使用基于角色和权限的访问控制来保护应用程序资源
带有单点登录(SSO)功能,可以将多个应用程序集成到一个中央身份验证系统
与其他Spring框架,如Spring MVC和Spring Boot,提供可定制的集成
正如其名字所示,Spring Security是将安全性融入了Spring生态系统中,这样就可以轻松地使用Spring的依赖注入和面向切面编程等强大功能来管理应用程序的安全性。
Spring Security的架构
Spring Security的架构如下所示:
+-----------------+
| Security Filter |
+-----------------+
|
+-----------------+
| Web Security |
+-----------------+
|
+-----------------+
| Authentication |
+-----------------+
|
+-----------------+
| Access Control |
+-----------------+
Security Filter:是整个Spring Security架构的基础。它是作为第一条链的Servlet过滤器。所有的安全相关操作都是在Security Filter之后执行的。
Web Security:是通过“HttpSecurity”对象实现的,它是框架的核心子系统,负责身份验证、授权和安全事件的处理等工作。Web Security通常与Spring MVC或Spring Boot集成使用。
Authentication:是指Spring Security处理身份验证的核心功能。它包括身份验证提供者、令牌和身份验证流程等组件。使用Spring Security,开发者可以选择多种身份验证方法,如HTTP Basic认证、表单登录、OpenID Connect等。
Access Control:是指Spring Security控制资源访问的核心功能。它使用“AccessDecisionManager”接口来决定用户是否有权限访问受保护的资源,同时支持基于角色和基于权限的访问控制。
Spring Security的主要特点
Spring Security具有以下主要特点:
支持多种身份验证方式:Spring Security支持多种身份验证方式,如HTTP身份验证、基本表单登录、OpenID Connect等。
用于访问控制的灵活而强大的体系结构:Spring Security提供了基于角色和基于权限的授权方式,可以轻松地控制和管理资源访问。
安全防范功能:Spring Security提供了多种安全防范功能,如CSRF防范、XSS防范、会话管理等,以保证应用程序的安全性。
可扩展性和可定制性:Spring Security是一个高度可定制的框架,允许应用程序开发人员对其进行扩展和自定义以满足自己的需求。
集成其他Spring框架:Spring Security可以轻松地与其他Spring框架,如Spring Boot和Spring MVC集成,使得使用Spring生态系统的开发人员无缝集成安全功能。
认证
认证是Spring Security框架的一个核心功能,它是通过身份验证来确定用户的身份。Spring Security支持多种身份验证方式,如HTTP Basic认证、表单登录、OpenID Connect等。
HTTP Basic认证
HTTP Basic认证是一种简单的身份验证方式,它将用户名和密码作为HTTP请求的头部信息发送给服务器进行验证。我们可以通过“HttpSecurity”对象实现HTTP Basic认证。
@Configuration
@EnableWebSecurity
public class HttpBasicSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
在上面的例子中,我们使用了Spring Security的Java配置方式来配置HTTP Basic认证。我们首先使用“authorizeRequests()”方法定义了所有请求都需要进行身份验证。然后,使用“httpBasic()”方法开启了HTTP Basic认证。
表单登录
在Spring Security中,我们也可以使用传统的用户名和密码表单登录来进行身份验证。通过表单登录,用户可以在Web应用程序的自定义登录页面中输入用户名和密码。
首先,我们需要使用“formLogin()”方法定义登录页面和处理URL:
@Configuration
@EnableWebSecurity
public class FormLoginSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/auth")
.defaultSuccessUrl("/home")
.failureUrl("/login?error=true")
.usernameParameter("username")
.passwordParameter("password");
}
}
在上面的例子中,我们使用“formLogin()”方法定义了登录页面和处理URL。我们首先允许所有用户访问"/login"页面,然后使用“loginPage()”方法定义了登录页面的URL;使用“loginProcessingUrl()”方法定义了登录处理URL。最后,使用“defaultSuccessUrl()”方法和“failureUrl()”方法定义登录成功和失败后的重定向页面。我们还可以使用“usernameParameter()”方法和“passwordParameter()”方法自定义表单中的用户名和密码输入框的name属性。
在Spring Security中,我们也可以使用@Component注解将LoginForm定义为一个Spring Bean,以方便使用。示例代码如下:
@Component
public class LoginForm extends UsernamePasswordAuthenticationToken {
private String username;
private String password;
public LoginForm(String username, String password) {
super(username, password);
this.username = username;
this.password = password;
}
@Override
public Object getCredentials() {
return password;
}
@Override
public Object getPrincipal() {
return username;
}
}
OpenID Connect
OpenID Connect是一种基于OAuth2协议的身份验证和授权协议,它适用于Web应用程序、移动应用程序和IoT设备等场景。Spring Security提供了对OpenID Connect的支持,我们可以使用“OAuth2LoginConfigurer”实现OpenID Connect认证。
首先,我们需要定义一个OAuth2 Client Registration:
@Configuration
public class OidcConfiguration {
@Bean
public ClientRegistration oidcClientRegistration() {
return ClientRegistration.withRegistrationId("oidc")
.clientId("my-client-id")
.clientSecret("my-client-secret")
.redirectUriTemplate("{baseUrl}/login/oauth2/code/{registrationId}")
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
.scope("openid", "profile", "email", "address", "phone")
.authorizationUri("https://accounts.google.com/o/oauth2/auth")
.tokenUri("https://www.googleapis.com/oauth2/v4/token")
.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
.userNameAttributeName(IdTokenClaimNames.SUB)
.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
.clientName("Google")
.build();
}
}
在上面的例子中,我们使用“ClientRegistration”对象定义了OpenID Connect客户端的信息,包括客户端ID、客户端密钥、重定向URI、授权类型、作用域、授权服务器URI、令牌URI、用户信息URI、用户名属性名称、JWK公钥集等。
然后,在Spring Security中,我们需要使用“OAuth2LoginConfigurer”配置OpenID Connect认证:
@Configuration
@EnableWebSecurity
public class OidcSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private ClientRegistration oidcClientRegistration;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2Login()
.clientRegistrationRepository(clientRegistrationRepository())
.userInfoEndpoint()
.oidcUserService(oidcUserService());
}
private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
return new OidcUserService();
}
private ClientRegistrationRepository clientRegistrationRepository() {
return new InMemoryClientRegistrationRepository(Collections.singletonList(oidcClientRegistration));
}
}
在上面的例子中,我们使用了“OAuth2LoginConfigurer”方法开启了OpenID Connect认证,同时使用了“clientRegistrationRepository()”方法和“oidcUserService()”方法配置OAuth2 Client Registration和OAuth2 User Service。
授权
Spring Security提供了基于角色和基于权限的访问控制,包括:
基于角色的访问控制
Spring Security使用角色来组织应用程序中的访问控制。我们可以使用“hasRole()”方法来实现基于角色的访问控制。示例代码如下:
@Configuration
@EnableWebSecurity
public class RoleBasedSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("{noop}admin123").roles("ADMIN")
.and()
.withUser("user").password("{noop}user123").roles("USER");
}
}
在上面的例子中,我们使用了“hasRole()”方法定义了"/admin/"和"/user/"路径需要ADMIN和USER角色才能访问。然后,我们使用“configureGlobal()”方法配置了用户信息,包括用户名、密码和角色。
基于权限的访问控制
Spring Security也支持基于权限的访问控制,我们可以使用“hasAuthority()”方法来实现基于权限的访问控制。示例代码如下:
@Configuration
@EnableWebSecurity
public class PermissionBasedSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasAuthority("ADMIN")
.antMatchers("/user/**").hasAuthority("USER")
.anyRequest().authenticated()
.and()
.formLogin();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("{noop}admin123").authorities("ADMIN")
.and()
.withUser("user").password("{noop}user123").authorities("USER");
}
}
在上面的例子中,我们使用了“hasAuthority()”方法定义了"/admin/"和"/user/"路径需要ADMIN和USER权限才能访问。然后,我们使用“configureGlobal()”方法配置了用户信息,包括用户名、密码和权限。
表达式语言
除了使用“hasRole()”方法和“hasAuthority()”方法来实现基于角色和基于权限的访问控制之外,Spring Security还支持使用表达式语言进行访问控制。我们可以使用“access()”方法来实现基于表达式语言的访问控制。示例代码如下:
@Configuration
@EnableWebSecurity
public class ExpressionBasedSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ADMIN')")
.antMatchers("/user/**").access("hasRole('USER') or hasIpAddress('127.0.0.1')")
.anyRequest().authenticated()
.and()
.formLogin();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("{noop}admin123").roles("ADMIN")
.and()
.withUser("user").password("{noop}user123").roles("USER");
}
}
在上面的例子中,我们使用了“access()”方法定义了"/admin/"和"/user/"路径的访问控制规则。其中,我们通过“hasRole()”表达式实现了对ADMIN角色的要求,同时通过“hasIpAddress()”表达式实现了对特定IP地址的允许。
LDAP身份验证
Spring Security也支持LDAP身份验证,我们可以使用“LdapAuthenticationConfigurer”来实现LDAP身份验证。示例代码如下:
@Configuration
@EnableWebSecurity
public class LdapSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource(contextSource())
.passwordCompare()
.passwordEncoder(new BCryptPasswordEncoder())
.passwordAttribute("userPassword");
}
private ContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://localhost:389");
contextSource.setBase("dc=springframework,dc=org");
contextSource.setUserDn("cn=admin,dc=springframework,dc=org");
contextSource.setPassword("adminpassword");
return contextSource;
}
}
在上面的例子中,我们使用了“ldapAuthentication()”方法启用了LDAP身份验证,并使用“userDnPatterns()”方法和“groupSearchBase()”方法定义了用户和组的搜索路径。然后,我们使用“contextSource()”方法定义了LDAP服务器的上下文源,包括LDAP服务器的URL、根目录、管理员用户名和密码等。
CSRF防范
Spring Security提供了CSRF防范功能,可以防止跨站点请求伪造攻击。我们可以通过“csrf()”方法开启CSRF防范:
@Configuration
@EnableWebSecurity
public class CsrfSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.logout()
.and()
.csrf();
}
}
在上面的例子中,我们使用了“csrf()”方法开启了CSRF防范。Spring Security默认情况下将会在所有POST、PUT、DELETE等非GET请求中自动包含CSRF令牌,以确保请求来自于合法的来源。
加密密码
Spring Security提供了多种加密算法来加密密码,包括BCrypt、SHA-256等。我们可以使用“PasswordEncoder”接口的实现类来进行密码加密和验证。示例代码如下:
@Configuration
@EnableWebSecurity
public class PasswordEncoderSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password(passwordEncoder().encode("admin123")).roles("ADMIN")
.and()
.withUser("user").password(passwordEncoder().encode("user123")).roles("USER");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
在上面的例子中,我们使用了“PasswordEncoder”接口的实现类“BCryptPasswordEncoder”来进行密码加密和验证,并在“configureGlobal()”方法中使用“passwordEncoder()”方法对密码进行加密。这样,在用户认证时,Spring Security会自动调用相应的密码加密算法对用户输入的密码进行加密和验证。
值得注意的是,在验证用户密码时,我们应该使用相应的密码加密算法来进行验证,而不是使用明文比较。这样可以保证密码的安全性。
来源:https://juejin.cn/post/7231746152903000122


猜你喜欢
- 1 简介Springboot是最简单的使用Spring的方式,而MongoDB是最流行的NoSQL数据库。两者在分布式、微服务架构中使用率极
- 本文以一个简单的实例来说明C#策略模式的实现方法,分享给大家供大家参考。具体实现方法如下:一般来说,当一个动作有多种实现方法,在实际使用时,
- springcloud微服务包含的技术种类众多,eureka作为其注册中心,一直处于主流,但在今年已经处于永久停更状态,但其优秀的能力还是值
- 通常用java来打包文件生成压缩文件后,有如下两个地方会出现乱码 :1、内容的中文乱码问题,这个问题网上很多人给出了解决方法,主要有两种方法
- 首先我们来理解下 * 的机制。Android的事件处理机制有两种:监听和回调。A基于监听的事件处理主要涉及三类对象:EventSource(
- 本文为大家分享了Android网络连接判断与相关处理,供大家参考,具体内容如下获取网络信息需要在AndroidManifest.xml文件中
- 本文实例为大家分享了Java实现简单幸运抽奖的具体代码,供大家参考,具体内容如下代码模块:User类:package test1;publi
- 本文实例讲述了Java线程之守护线程(Daemon)用法。分享给大家供大家参考。具体如下:守护线程(Daemon)Java有两种Thread
- Android 自带的资源字符串资源:定义字符串资源在 <string.xml >;在JAVA 中 使用字符串资源,通过 get
- 摘要在使用java做后台站点的开发张,图表和报表功能都是不可或缺 的。本文推荐了8款最精彩实用的Java图表应用,大部分图表应用的功能都类似
- 前言:反射(Reflection)是.NET提供给开发者的一个强大工具,尽管作为.NET框架的使用者,很多时候不会用到反射。但在一些情况下,
- 一、问题描述Android studio导入一个项目报一堆错误:Process: xhs.com.xhswelcomeanim, PID:
- 介绍Java状态模式(State Pattern)是一种面向对象的设计模式,它将对象的状态封装成独立的状态对象,并将对象的行为与状态对象解耦
- 本文实例讲述了C#利用原图和水印图的重叠简单实现水印的方法。分享给大家供大家参考,具体如下:图片操作类/// <summary>
- 开始用的.net 自带的DataContractJsonSerializer进行序列化和反序列化,当代码写完以后,调试,我X(原谅我的脏话,
- log4j2支持日志的异步打印,日志异步输出的好处在于,使用单独的进程来执行日志打印的功能,可以提高日志执行效率,减少日志功能对正常业务的影
- 本文实例讲述了C#实现的微信网页授权操作逻辑封装。分享给大家供大家参考,具体如下:一、微信网页授权登录前提:1.已经获取的接口权限,如果是测
- 目录获取程序下面的文件获取临时目录下的文件获取程序下面的文件首先我们创建了实例解决方案:其中调用链是:Main.Shell->FooA
- 前言 短时间提升自己最快的手段就是背面试题,最近总结了Java常用的面试题,分享给大家,希望大家都能圆梦大厂,加油,我命由我不由天
- 最近在看《.NET游戏编程入门经典 C#篇》 第一章介绍了如何制作俄罗斯方块,自己试了试按照书上的步骤,可算是完成了。于是写下这篇文章留作纪