包含流程图+各个模块代码+配置文件+jar包
流程图
EmployeeController 中的方法:
// 登录校验
@RequestMapping("/login")
public String main(Model model, HttpServletRequest request) throws Exception {
// 从request域获得customRealm传来的错误信息
String exceptionName = (String) request.getAttribute("shiroLoginFailure");
if (exceptionName != null) {// 处理错误的消息
// 1.账号错误UnknownAccountException
if (UnknownAccountException.class.getName().equals(exceptionName)) {
// 在login设置错误信息
model.addAttribute("errorMsg", "账号错误");
}
// 2.密码错误incorrectCrend
if (IncorrectCredentialsException.class.getName().equals(exceptionName)) {
// 在login设置错误信息
model.addAttribute("errorMsg", "密码错误");
}
}
return "login";
}
//主页面
@RequestMapping("/main")
public String main(Model model) throws Exception {
// 从shiro的session中获得employee
Subject subject = SecurityUtils.getSubject();
// 取身份信息
Employee employee = (Employee) subject.getPrincipal();
//通过model传到页面
model.addAttribute("employee", employee);
return "index";
}
//退出登录
@RequestMapping("/logout")
public String logout(HttpSession session)throws Exception{
//session失效
session.invalidate();
return "login";
}
CustomRealm:
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//登录功能
//获取用户输入的账号
String userName = (String) token.getPrincipal();
Employee loginEmp = null;
try {
loginEmp = employeeService.findEmployeeByUserName(userName);
if(loginEmp==null){
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
String password_db = loginEmp.getPassword();
String salt = loginEmp.getSalt();
//构建Employee封装用户对象
Employee employee = new Employee();
employee.setId(loginEmp.getId());
employee.setName(loginEmp.getName());
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(employee, password_db,ByteSource.Util.bytes(salt), "CustomRealm");
return info;
}
EmployeeServiceImpl
@Service
public class EmployeeServiceImpl implements EmployeeService{
//登录
@Autowired
private EmployeeMapper employeeMapper;
@Override
public Employee findEmployeeByUserName(String userName) {
EmployeeExample example = new EmployeeExample();
EmployeeExample.Criteria criteria = example.createCriteria();
criteria.andNameEqualTo(userName);
List<Employee> list = employeeMapper.selectByExample(example);
if (list != null && list.size()>0) {
return list.get(0);
}
return null;
}
此时页面会有bug,bug解决:在自定义表单验证过滤器CustomFormAuthenticationFilter中加一个方法
@Override
protected boolean onLoginSuccess(AuthenticationToken token,Subject subject,ServletRequest request,ServletResponse response) throws Exception{
WebUtils.getAndClearSavedRequest(request);
WebUtils.redirectToSavedRequest(request, response, "/main");
return false;
}
配置文件:
链接:https://pan.baidu.com/s/1O6ERzxo9TqenHnCCeK1Yww
提取码:k7jj
ssm+activiti5+shiro-web jar包:
链接:https://pan.baidu.com/s/10I7F78L3scvTnLCJg25WQg
提取码:87b2