Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架,它是用于保护基于Spring的应用程序的实际标准。java
Spring Security是一个框架,致力于为Java应用程序提供身份验证和受权。与全部Spring项目同样,Spring Security的真正强大之处在于能够轻松扩展以知足自定义要求。git
更多信息能够查看官网:https://spring.io/projects/spring-securitygithub
新建一个SpringBoot的web项目spring-boot-security。web
pom文件中不引入Spring Security,而后新建一个controller:spring
@RestController public class AppController { @GetMapping("/hello") public String hello() { return "Hello,spring security!"; } }
而后打开浏览器访问:http://localhost:8080/hello,成功后返回:浏览器
Hello,spring security!
pom文件中引入Spring Security的starter:springboot
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
打开浏览器再次访问http://localhost:8080/hello,会被重定向到登陆页http://localhost:8080/login,截图以下:mybatis
要登陆系统,咱们须要知道用户名和密码,Spring Security默认的用户名是user,项目启动的时候会生成默认密码(在启动日志中能够看到),输入用户名和密码后就能够访问/hello接口了。架构
固然也能够自定义用户名密码,在配置文件添加以下内容便可:app
spring.security.user.name=java_suisui spring.security.user.password=123456
上面说过Spring Security的功能有“认证”和“受权”,下面经过一个简单的例子实现下自定义的认证和受权。
假设系统中有两个角色:
按照下面步骤操做便可。
对于用户名、密码、登陆页面、访问权限等均可以在 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"); } }
内存用户验证时,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登陆后访问/admin,会报403错误,具体错误信息以下:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Nov 19 16:26:28 CST 2019 There was an unexpected error (type=Forbidden, status=403). Forbidden
结果和咱们预期的一致,说明简单的自定义认证和受权功能已经实现了。
完整源码地址: https://github.com/suisui2019/springboot-study
推荐阅读
1.一分钟带你学会利用mybatis-generator自动生成代码!
3.SpringBoot系列-整合Mybatis(注解方式)
4.SpringBoot系列-整合Mybatis(XML配置方式)
Java碎碎念,一个坚持原创的公众号,为您提供一系列系统架构、微服务、Java、SpringBoot、SpringCloud等高质量技术文章。
若是以为文章不错,但愿能够随手转发或者”在看“哦,很是感谢哈!
关注下方公众号后回复「1024」,有惊喜哦!
本文由博客一文多发平台 OpenWrite 发布!