先来无事看看acegi的登陆过滤器 写下来当作备忘吧
主要的类是AuthenticationProcessingFilter 继承了AbstractProcessingFilter 这要的逻辑都在后面这个类中
让我们看看核心代码吧
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
if (!(request instanceof HttpServletRequest)) {
throw new ServletException("Can only process HttpServletRequest");
}
if (!(response instanceof HttpServletResponse)) {
throw new ServletException("Can only process HttpServletResponse");
}
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
if (requiresAuthentication(httpRequest, httpResponse)) {
if (logger.isDebugEnabled()) {
logger.debug("Request is to process authentication");
}
Authentication authResult;
//下面才是重点 上面都是些基本检查
try {
onPreAuthentication(httpRequest, httpResponse);
authResult = attemptAuthentication(httpRequest);//这个方法就是去登陆了 就是调用dao检查用户名密码 登陆不成功将抛出异常
}
catch (AuthenticationException failed) {
// Authentication failed
unsuccessfulAuthentication(httpRequest, httpResponse, failed);
return;
}
// Authentication success
if (continueChainBeforeSuccessfulAuthentication) {
chain.doFilter(request, response);
}
successfulAuthentication(httpRequest, httpResponse, authResult);
return;
}
chain.doFilter(request, response);
}
看一些登陆成功后 做些什么
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
Authentication authResult) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("Authentication success: " + authResult.toString());
}
//把用户信息保存到SecurityContextHolder中1980
SecurityContextHolder.getContext().setAuthentication(authResult);
if (logger.isDebugEnabled()) {
logger.debug("Updated SecurityContextHolder to contain the following Authentication: '" + authResult + "'");
}
//转到目标页面 即登陆成功页面
String targetUrl = determineTargetUrl(request);
if (logger.isDebugEnabled()) {
logger.debug("Redirecting to target URL from HTTP Session (or default): " + targetUrl);
}
onSuccessfulAuthentication(request, response, authResult);
rememberMeServices.loginSuccess(request, response, authResult);
// Fire event
if (this.eventPublisher != null) {
eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
}
sendRedirect(request, response, targetUrl);
}
分享到:
相关推荐
开发者在学习Acegi的过程中遇到困难,需要保持耐心和毅力。这不仅能够提升个人技能,也能为后续的项目带来宝贵的经验。随着时间的推移,不断学习新的安全框架和最佳实践,以适应快速变化的技术环境。 综上所述,这...
这个登陆系统是基于这三个框架构建的,下面将详细解释每个框架及其在登录系统中的作用。 **Struts** Struts是一个开源的MVC(Model-View-Controller)框架,用于构建基于Java EE web应用程序。在登录系统中,Struts...
此外,Spring Security(前身是Acegi Security)可以进一步加强安全性,提供角色授权、访问控制等功能。 Struts 2.3 是一个基于MVC设计模式的Java Web框架,用于构建可维护性和可扩展性高的应用。在登录验证场景中...
与其他框架相比,它提供了各种Web系统开发过程中都需要开发的一些功能,如登陆、用户密码加密,用户管理、根据不同的用户可以展现不同的菜单,同时还带有代码生成器,自动生成40%-60%左右的代码,自带了默认的一些...
这个"基于struts-spring-hibernate的轻量级登陆J2EE开发"项目着重于实现一个用户登录系统,以下是关于这个项目的关键知识点: 1. **Struts框架**:Struts是Apache组织的一个开源MVC框架,它为Java Web应用提供了一...
此外,Spring的安全模块(Spring Security,原名Acegi)可以用来实现更复杂的权限控制和认证,但在这个简单的例子中,可能只是通过Spring的AOP(面向切面编程)来实现基本的登录检查。 再者,Hibernate是Java的持久...
Spring Security 的前身是 Acegi Security,在被纳入 Spring 项目之后,它经历了多个版本的迭代。Spring Security 3.0 是一个重要的里程碑,它引入了许多新特性,并且对原有的架构进行了优化。 **1.3 发行版本号** ...