网站首页 > 文章精选 正文
一、什么是Spring Security?
Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架,它是用于保护基于Spring的应用程序的实际标准。
Spring Security是一个框架,致力于为Java应用程序提供身份验证和授权。与所有Spring项目一样,Spring Security的真正强大之处在于可以轻松扩展以满足自定义要求。
更多信息可以查看官网:https://spring.io/projects/spring-security
二、Spring Security的主要功能
- 认证:验证用户名和密码是否合法(是否系统中用户)
- 授权:是系统用户不代表你能使用某些功能,因为你可能没有权限
- 防御会话固定,点击劫持,跨站点请求伪造等攻击
- Servlet API集成
- 与Spring Web MVC的可选集成
三、快速入门
新建一个SpringBoot的web项目spring-boot-security。
案例1:接口不添加保护
pom文件中不引入Spring Security,然后新建一个controller:
@RestController public class AppController { @GetMapping("/hello") public String hello() { return "Hello,spring security!"; } }
然后打开浏览器访问:http://localhost:8080/hello,成功后返回:
Hello,spring security!
案例2:接口添加保护
- pom文件添加依赖
pom文件中引入Spring Security的starter:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
- 访问接口
打开浏览器再次访问http://localhost:8080/hello,会被重定向到登录页http://localhost:8080/login,截图如下:
要登录系统,我们需要知道用户名和密码,Spring Security默认的用户名是user,项目启动的时候会生成默认密码(在启动日志中可以看到),输入用户名和密码后就可以访问/hello接口了。
当然也可以自定义用户名密码,在配置文件添加如下内容即可:
spring.security.user.name=java_suisui spring.security.user.password=123456
四、自定义认证和授权
上面说过Spring Security的功能有“认证”和“授权”,下面通过一个简单的例子实现下自定义的认证和授权。
假设系统中有两个角色:
- ADMIN 可以访问/admin下的资源
- USER 可以访问/user下的资源
按照下面步骤操作即可。
- 新建一个配置类
对于用户名、密码、登录页面、访问权限等都可以在 WebSecurityConfigurerAdapter 的实现类中配置。
WebSecurityConfig代码如下:
/** * 配置类 * @Author java_suisui * */ @EnableWebSecurity @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //配置内存中的 用户名、密码和角色 auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("user").password("123456").roles("USER"); auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("admin").password("123456").roles("ADMIN"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login").permitAll() .antMatchers("/user").hasRole("USER") //访问 /user这个接口,需要有USER角色 .antMatchers("/admin").hasRole("ADMIN") .anyRequest().authenticated() //剩余的其他接口,登录之后就能访问 .and() .formLogin().defaultSuccessUrl("/hello"); } }
- 创建PasswordEncorder的实现类
内存用户验证时,Spring Boot 2.0以上版本引用的security 依赖是 spring security 5.X版本,此版本需要提供一个PasswordEncorder的实例。
MyPasswordEncoder代码如下:
public class MyPasswordEncoder implements PasswordEncoder { @Override public String encode(CharSequence rawPassword) { return rawPassword.toString(); } @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { return encodedPassword.equals(rawPassword); } }
- 登录验证
浏览器打开http://localhost:8080/login,
- 使用user登录,可以访问/user
- 使用admin登录,可以访问/admin
如果使用user登录后访问/admin,会报403错误,具体错误信息如下:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Forbidden, status=403). Forbidden
结果和我们预期的一致,说明简单的自定义认证和授权功能已经实现了。
猜你喜欢
- 2024-12-30 简单的使用SpringBoot整合SpringSecurity
- 2024-12-30 Spring Security 整合OAuth2 springsecurity整合oauth2+jwt+vue
- 2024-12-30 DeepSeek-Coder-V2震撼发布,尝鲜体验
- 2024-12-30 一个数组一行代码,Spring Security就接管了Swagger认证授权
- 2024-12-30 简单漂亮的(图床工具)开源图片上传工具——PicGo
- 2024-12-30 Spring Boot(十一):Spring Security 实现权限控制
- 2024-12-30 绝了!万字搞定 Spring Security,写得太好了
- 2024-12-30 SpringBoot集成Spring Security springboot集成springsecurity
- 2024-12-30 SpringSecurity密码加密方式简介 spring 密码加密
- 2024-12-30 Spring cloud Alibaba 从入门到放弃
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 计算机网络的拓扑结构是指() (45)
- 稳压管的稳压区是工作在什么区 (45)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)