赞
踩
目录

创建springboot项目,版本为2.1.1.RELEASE,引入依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-security</artifactId>
- </dependency>
编写Controller
- @RestController
- @RequestMapping("/test")
- public class HelloController {
-
- @GetMapping("/hello")
- public String test() {
- return "hello, security";
- }
- }
修改启动端口
- server:
- port: 8111
启动项目,访问/test/hello,发现调整到了/login,也就是需要SpringSecurity的认证。需要用户名和密码,用户名默认为user,密码在控制台


以上就是SpringSecurity初体验
- org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFil
- ter
- org.springframework.security.web.context.SecurityContextPersistenceFilter
- org.springframework.security.web.header.HeaderWriterFilter
- org.springframework.security.web.csrf.CsrfFilter
- org.springframework.security.web.authentication.logout.LogoutFilter
- org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
- org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter
- org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter
- org.springframework.security.web.savedrequest.RequestCacheAwareFilter
- org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter
- org.springframework.security.web.authentication.AnonymousAuthenticationFilter
- org.springframework.security.web.session.SessionManagementFilter
- org.springframework.security.web.access.ExceptionTranslationFilter
- org.springframework.security.web.access.intercept.FilterSecurityInterceptor
比较重要的有三个过滤器:
只有一个loadUserByUsername方法,
返回值 UserDetails :是系统默认的用户“主体”
方法参数 username :表示用户名。此值是客户端表单传递过来的数据。默认情况下必须叫 username,否则无法接收。

以后我们只需要使用 User 这个实体类即可!

- // 表示把参数按照特定的解析规则进行解析
- String encode(CharSequence rawPassword);
- // 表示验证从存储中获取的编码密码与编码后提交的原始密码是否匹配。如果密码匹配,则返回 true;如果不匹配,则返回 false。第一个参数表示需要被解析的密码。第二个参数表示存储的密码。
- boolean matches(CharSequence rawPassword, String encodedPassword);
- // 表示如果解析的密码能够再次进行解析且达到更安全的结果则返回 true,否则返回false。默认返回false。
- default boolean upgradeEncoding(String encodedPassword) {
- return false;
- }
- @Test
- public void test01(){
- // 创建密码解析器
- BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
- // 对密码进行加密
- String atguigu = bCryptPasswordEncoder.encode("atguigu");
- // 打印加密之后的数据
- System.out.println("加密之后数据:\t"+atguigu);
- //判断原字符加密后和加密之前是否匹配
- boolean result = bCryptPasswordEncoder.matches("atguigu", atguigu);
- // 打印比较结果
- System.out.println("比较结果:\t"+result);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。