标签:username web 入门 基本原理 request springframework SpringSecurity org security
1、入门案例
1.1、创建SpringBoot项目
1.2、勾选对应的maven依赖
这里一些依赖可以没有,最主要是要有Web和Security两个依赖即可!
1.3、编写Controller路由
@Controller
public class RouterController {
@RequestMapping(value = {"/index","/","/index.html"})
@ResponseBody
public String success(){
return "Hello SpringSecurity";
}
}
1.4、启动项目
-
启动项目之后会发现自动来到了登录页面,这个登录页面并不是我们写的,是由Security自带的并且现在说明Security已经开启了用户认证。
-
可以在控制台拿到密码(随机),用户名为user;使用密码登录之后就能看到页面了!
2、基本原理
2.1、Security装载过程
- 根据SpringBoot自动装配原理可以得知,SpringBoot在启动时会自动加载spring-boot-autoconfigure包下的spring.factories中的配置,其中有做安全认证的security组件!
- 虽然自动装配了security的组件,但是并没有完全生效,还需要导入security的依赖,这时才会根据@Condition进行条件装配bean。
2.2、基本原理
-
SpringSecurity的本质是Interceptor拦截器 + Filter过滤器的执行链,而拦截器的本质是AOP。
-
可以在security包中看到大量的拦截器类、过滤器类等等,它们分别负责不同的功能。
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
2.3、UsernamePasswordAuthenticationFilter过滤器
- 这是Security中的一个过滤器,用户对登录/login请求进行拦截验证的实现类。
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {
if (this.postOnly && !request.getMethod().equals("POST")) {
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
}
String username = obtainUsername(request);
username = (username != null) ? username : "";
username = username.trim();
String password = obtainPassword(request);
password = (password != null) ? password : "";
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
// Allow subclasses to set the "details" property
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
- 其中核心的方法是authenticate()方法,在这里对用户提交的账号和密码进行验证。
标签:username,web,入门,基本原理,request,springframework,SpringSecurity,org,security 来源: https://blog.csdn.net/weixin_43808717/article/details/120090649
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。