spring security 简介
spring security 是一个功能强大且高度可定制的身份验证和访问控制框架,它是 spring 生态系统中的一部分,主要用于保护基于 spring 的应用程序。spring security 提供了全面的安全解决方案,包括身份验证、授权、攻击防护等功能。
spring security 的核心功能包括:
- 身份验证(authentication):验证用户身份,确保用户是他们声称的那个人。
- 授权(authorization):控制用户对资源的访问权限。
- 攻击防护:防止常见的 web 攻击,如 csrf、xss 等。
spring security 核心概念
1. securitycontext
securitycontext 是 spring security 中存储当前用户安全信息的上下文对象。它包含了当前用户的 authentication 对象。
securitycontext context = securitycontextholder.getcontext(); authentication authentication = context.getauthentication();
2. authentication
authentication 对象表示用户的身份信息,包括用户的主体(principal)、凭证(credentials)和权限(authorities)。
authentication authentication = new usernamepasswordauthenticationtoken("user", "password", authorities);3. userdetails
userdetails 接口表示用户的详细信息,spring security 使用它来加载用户信息。
public class customuserdetails implements userdetails {
private string username;
private string password;
private collection<? extends grantedauthority> authorities;
// getters and setters
}4. userdetailsservice
userdetailsservice 接口用于加载用户信息,通常用于从数据库或其他数据源中加载用户信息。
@service
public class customuserdetailsservice implements userdetailsservice {
@override
public userdetails loaduserbyusername(string username) throws usernamenotfoundexception {
// load user from database
return new customuserdetails(username, "password", authorities);
}
}5. grantedauthority
grantedauthority 表示用户的权限,通常是一个字符串,如 role_admin。
grantedauthority authority = new simplegrantedauthority("role_admin");spring security 配置
1. 基本配置
spring security 的基本配置可以通过 websecurityconfigureradapter 类来实现。
@configuration
@enablewebsecurity
public class securityconfig extends websecurityconfigureradapter {
@override
protected void configure(httpsecurity http) throws exception {
http
.authorizerequests()
.antmatchers("/public/**").permitall()
.anyrequest().authenticated()
.and()
.formlogin()
.loginpage("/login")
.permitall()
.and()
.logout()
.permitall();
}
@override
protected void configure(authenticationmanagerbuilder auth) throws exception {
auth.inmemoryauthentication()
.withuser("user").password("{noop}password").roles("user")
.and()
.withuser("admin").password("{noop}admin").roles("admin");
}
}2. 自定义登录页面
可以通过 formlogin().loginpage("/login") 来指定自定义的登录页面。
@override
protected void configure(httpsecurity http) throws exception {
http
.authorizerequests()
.anyrequest().authenticated()
.and()
.formlogin()
.loginpage("/login")
.permitall();
}3. 自定义注销
可以通过 logout() 方法来配置注销行为。
@override
protected void configure(httpsecurity http) throws exception {
http
.logout()
.logouturl("/logout")
.logoutsuccessurl("/login?logout")
.invalidatehttpsession(true)
.deletecookies("jsessionid");
}认证与授权
1. 基于内存的认证
可以通过 authenticationmanagerbuilder 配置基于内存的认证。
@override
protected void configure(authenticationmanagerbuilder auth) throws exception {
auth.inmemoryauthentication()
.withuser("user").password("{noop}password").roles("user")
.and()
.withuser("admin").password("{noop}admin").roles("admin");
}2. 基于数据库的认证
可以通过 userdetailsservice 配置基于数据库的认证。
@autowired
private datasource datasource;
@override
protected void configure(authenticationmanagerbuilder auth) throws exception {
auth.jdbcauthentication()
.datasource(datasource)
.usersbyusernamequery("select username, password, enabled from users where username=?")
.authoritiesbyusernamequery("select username, authority from authorities where username=?");
}3. 基于角色的授权
可以通过 hasrole() 或 hasauthority() 方法进行基于角色的授权。
@override
protected void configure(httpsecurity http) throws exception {
http
.authorizerequests()
.antmatchers("/admin/**").hasrole("admin")
.antmatchers("/user/**").hasrole("user")
.anyrequest().authenticated();
}密码加密
spring security 提供了多种密码加密方式,如 bcryptpasswordencoder、pbkdf2passwordencoder 等。
@bean
public passwordencoder passwordencoder() {
return new bcryptpasswordencoder();
}在配置认证时,可以使用 passwordencoder 对密码进行加密。
@override
protected void configure(authenticationmanagerbuilder auth) throws exception {
auth.userdetailsservice(userdetailsservice).passwordencoder(passwordencoder());
}csrf 防护
spring security 默认启用了 csrf 防护,可以通过 csrf().disable() 来禁用。
@override
protected void configure(httpsecurity http) throws exception {
http.csrf().disable();
}oauth2 集成
spring security 提供了对 oauth2 的支持,可以通过 @enableoauth2client 注解启用 oauth2 客户端。
@configuration
@enableoauth2client
public class oauth2config {
@bean
public oauth2resttemplate oauth2resttemplate(oauth2clientcontext oauth2clientcontext,
oauth2protectedresourcedetails details) {
return new oauth2resttemplate(details, oauth2clientcontext);
}
}spring security 与 jwt
jwt(json web token)是一种用于身份验证的令牌,spring security 可以与 jwt 集成来实现无状态的身份验证。
public class jwttokenutil {
private string secret = "secret";
public string generatetoken(userdetails userdetails) {
return jwts.builder()
.setsubject(userdetails.getusername())
.setissuedat(new date())
.setexpiration(new date(system.currenttimemillis() + 1000 * 60 * 60 * 10))
.signwith(signaturealgorithm.hs512, secret)
.compact();
}
public string getusernamefromtoken(string token) {
return jwts.parser()
.setsigningkey(secret)
.parseclaimsjws(token)
.getbody()
.getsubject();
}
}spring security 与 thymeleaf
spring security 可以与 thymeleaf 集成,在模板中使用安全相关的标签。
<div sec:authorize="isauthenticated()">
welcome <span sec:authentication="name"></span>
</div>
<div sec:authorize="hasrole('admin')">
<a href="/admin" rel="external nofollow" >admin panel</a>
</div>spring security 测试
spring security 提供了 @withmockuser 注解来模拟用户进行测试。
@test
@withmockuser(username = "user", roles = {"user"})
public void testuseraccess() {
// test user access
}常见问题与解决方案
1. 403 forbidden
- 原因:用户没有访问该资源的权限。
- 解决方案:检查用户的角色和权限配置。
2. 无法登录
- 原因:密码不匹配或用户不存在。
- 解决方案:检查用户信息和密码加密方式。
3. csrf token 缺失
- 原因:表单提交时未包含 csrf token。
- 解决方案:确保表单中包含 csrf token。
<input type="hidden" name="${_csrf.parametername}" value="${_csrf.token}"/>到此这篇关于spring security常见问题及解决方案的文章就介绍到这了,更多相关spring security配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论