1 apache shiro框架简介
官网:shiro.apache.org
l shiro框架的核心功能:
认证
授权
会话管理
加密
l shiro框架认证流程
Application Code:应用程序代码,由开发人员负责开发的
Subject:框架提供的接口,代表当前用户对象
SecurityManager:框架提供的接口,代表安全管理器对象
Realm:可以开发人员编写,框架也提供一些,类似于Dao,用于访问权限数据
好了,简单的介绍说完了,接下来开始整合:
第一步:导入maven依赖(因为我的是maven工程)
第二步:在web.xml中添加过滤器
第三步:编写一个自定义的realm
package com.itpengwei.bos.service.realm; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; import com.itpengwei.bos.mapper.UserMapper; import com.itpengwei.bos.pojo.User; /** * 自定义realm * * @author pengwei * */ public class GloabRealm extends AuthorizingRealm { @Autowired private UserMapper userMapper; // 授权方法 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { return null; } // 认证方法 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.out.println("------自定义realm执行了认证方法......"); UsernamePasswordToken mytoken = (UsernamePasswordToken) token; if (StringUtils.isNotBlank(mytoken.getUsername())) { // 1,取出用户名 String username = mytoken.getUsername(); // 2,去数据库中查询用户信息根据用户名 User user = userMapper.findUserByUsername(username); if (user == null) { // 3,用户不存在,返回null框架抛出异常,到controller进行统一处理 return null; } // 4,到这里说明数据已经查询到了,让shiro进行对页面提交的密码和数据库中的密码进行校验 // 第一个:参数数据库查询出来的用户对象,第二个:数据库密码,第三个参数:当前Realm名字 AuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName()); return info; } // 用户名为空,返回null抛出异常提示用户 return null; } }
第四步:创建一个spring的配置文件名字随自己的习惯(我这里用的是applicationContext-shiro.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 配置shiro框架的过滤器工厂bean --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/login.jsp" /> <property name="successUrl" value="/successUrl.jsp" /> <property name="unauthorizedUrl" value="/unauthorizedUrl.jsp" /> <!-- 指定拦截策略 --> <property name="filterChainDefinitions"> <value> /css/**=anon /js/**=anon /images/**=anon /login.jsp*=anon //user/login.action=anon /validatecode.jsp*=anon /*=authc </value> </property> </bean> <!-- 配置安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="gloabRealm"></property> </bean> <!-- 注册自定义realm --> <bean id="gloabRealm" class="com.itpengwei.bos.service.realm.GloabRealm" /> </beans>
第五步:在Controller中使用shiro框架进行认证
// 用户登录使用shiro进行验证 @RequestMapping(value = "/user/login", method = RequestMethod.POST) public String checkLogin1(User user, String checkcode, HttpServletRequest request) { HttpSession session = request.getSession(); if (StringUtils.isNotBlank(checkcode)) { String key = (String) session.getAttribute("key"); if (checkcode.equals(key)) { // 使用shiro框架的方式进行认证 Subject subject = SecurityUtils.getSubject();// 获取当前用户登录对象,现在状态为“未认证” // 用户名和密码令牌 AuthenticationToken token = new UsernamePasswordToken(user.getUsername(), MD5Utils.md5(user.getPassword())); try { subject.login(token); User loginUser = (User) subject.getPrincipal(); session.setAttribute("loginUser", loginUser); // 登录成功,跳转到首页 return "pages/common/index"; } catch (Exception e) { e.printStackTrace(); // 验证失败,重定向到登录页面 return "redirect:" + request.getContextPath() + "/login.jsp"; } } } // 登录失败重定向到登录页面 return "redirect:" + request.getContextPath() + "/login.jsp"; }好了,结束,是不是很简单,总共就五步,授权加认证请看这篇文章 授权加认证