shiro+spring
2024-09-01 08:42:26
公司自用的管理系统使用了shiro,但是对于这个登录页面跳转、登录的过程逻辑以及登录成功或者失败的跳转页面一直不理解,查看相关文档资料,整理出一些结果并本地调试测试,记录下备以后回顾之用。
对于spring+shiro,很多文档都是:搭建环境,然后配置web.xml,shiro spring配置文件,开发自己的realm、shiro的过滤器链以及spring controller等等内容。实际开发中,也是按照这套路来使用的。但是:对于localhost/xxx/login登录请求页面跳转,怎么感觉是直接请求spring而未通过shiro过滤器?
本地web.xml配置文件部分截图:
shiro配置:
spring mvc作为请求控制器,shiro作为权限过滤器,shiro对所有请求url进行判断,并指定相关的过滤器,例如/xxx/login指定的是authc过滤器(验证,这些页面必须验证后才能访问,也就是我们说的登录后才能访问),过滤器逻辑如果抛出异常表示认证失败,过滤器会转向shiroFilter配置的loginUrl地址,认证成功会转向配置的successUrl地址。而转向过后的逻辑将是由springmvc来控制。
登录时验证:
/**
*
*/
package com.autrade.xxx.shiro; import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Service; import com.autrade.sptmasterweb.util.FailCacheUtils; /**
* 表单验证(包含验证码)过滤类 *
*/
@Service
public class FormAuthenticationFilter extends org.apache.shiro.web.filter.authc.FormAuthenticationFilter { public static final String DEFAULT_MESSAGE_PARAM = "message"; @Override
protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
AuthenticationToken token = createToken(request, response);
if (token == null) {
String msg = "createToken method implementation returned null. A valid non-null AuthenticationToken " +
"must be created in order to execute a login attempt.";
throw new IllegalStateException(msg);
}
try {
Long lockTime = FailCacheUtils.isLocked(token.getPrincipal().toString());
if (lockTime>0){
request.setAttribute(DEFAULT_MESSAGE_PARAM, "错误次数过多,请稍后再试!");
return true;
}
Subject subject = getSubject(request, response); //代表当前前台传过来的用户
subject.login(token);
return onLoginSuccess(token, subject, request, response);
} catch (AuthenticationException e) {
return onLoginFailure(token, e, request, response);
}
}
/**
* 登录失败调用事件
*/
@Override
protected boolean onLoginFailure(AuthenticationToken token,
AuthenticationException e, ServletRequest request, ServletResponse response) {
String className = e.getClass().getName(), message = "";
if (IncorrectCredentialsException.class.getName().equals(className)
|| UnknownAccountException.class.getName().equals(className)){
message = "用户或密码错误, 请重试.";
}
else if (e.getMessage() != null && StringUtils.indexOf(e.getMessage(), "msg:")!=-1){
message = StringUtils.replace(e.getMessage(), "msg:", "");
}
else{
message = "系统出现点问题,请稍后再试!";
e.printStackTrace(); // 输出到控制台
}
request.setAttribute(getFailureKeyAttribute(), className);
request.setAttribute(DEFAULT_MESSAGE_PARAM, message);
String username = WebUtils.getCleanParam(request, FormAuthenticationFilter.DEFAULT_USERNAME_PARAM);
FailCacheUtils.addFailCnt(username);
return true;
} @Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request,
ServletResponse response) throws Exception {
String username = WebUtils.getCleanParam(request, FormAuthenticationFilter.DEFAULT_USERNAME_PARAM);
FailCacheUtils.remave(username);
return super.onLoginSuccess(token, subject, request, response);
} }
自定义realm中:
认证:
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
//token令牌,也就相当于一张表格,你要去验证,你就得填个表,里面写好用户名密码,交给公安局的同志给你验证
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
InternalUserDownEntity userAcctInfoEntity = internalUserService.findInternalUserInfoByUserName(token.getUsername());if (userAcctInfoEntity!=null)
{
//用户我们可以前面的令牌也就是我说的表格来取的,我们前台提交给公安同志一个表格,里面有用户密码,
//但需要注意的是,我们在这里并不把表格中的用户密码路数据库中的用户密码进行比较,我们只是根据表格中的用户名把密码查出来,
//然后把数据库的用户密码放在一个SimpleAuthenticationInfo对象中返回即可,这位公安同志不负责验证,只负责验证的材料
InternalUserAuthEntity userAuthEntity = new InternalUserAuthEntity();
userAuthEntity.setUserId(userAcctInfoEntity.getUserId());
ShiroUser shiroUser = new ShiroUser(userAcctInfoEntity.getUserId(), token.getUsername(),token.getUsername(), userAuthEntity);
return new SimpleAuthenticationInfo(shiroUser,userAcctInfoEntity.getPassWord(), getName());
}
else
{
return null;
}
}
以上准备好比对数据,什么时候验证呢?
(1)executeLogin中调用subject.isAuthenticated()时
(2)由于之前的shiro spring配置文件中配置了/login = authc, 配了authc过滤器,shiro会自动调用subject.isAuthenticated()方法完成验证对比。
最新文章
- Sql Server函数全解(三)数据类型转换函数和文本图像函数
- LoadRunner 场景运行error的几种情况
- 暑假集训(2)第八弹 ----- Hero(hdu4310)
- 【Xamarin 开发 IOS --使用 Storyboard Segue 实作 UIViewController 的切换 (实例)】
- WindowsService服务的C#实现
- ACM-ICPC之路
- C# 设计模式之空对象模式
- [Swift]LeetCode56. 合并区间 | Merge Intervals
- 记录一次程序输出和DB查询不匹配的问题
- Node学习笔记(二)
- oracle 11gR2 ASM添加和删除磁盘
- 使用Hexo+Github搭建属于自己的博客(基础)
- 实践 : Ubuntu 上 Testlink 部署
- 在centos中修改yum源为阿里源
- bat 变量作用域
- Scrapy是什么
- Mysql索引机制B+Tree
- IO流-递归遍历目录下指定后缀名结尾的文件名称
- CentOS Linux 下安装Samba
- nginx和php之间是怎样通信的呢?