Shiro 整合SpringMVC 并且实现权限管理,登录和注销
Apache Shiro是Java的一个安全框架。目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大,但是在实际工作时可能并不需要那么复杂的东西,所以使用小而简单的Shiro就足够了。
因为我总结的是使用SpringMVC和Apache Shiro整合,注重的是整合和使用,至于基础,我这里就不细说了.按照惯例,既然是需要创建项目,那么我们首先需要JAR包,Apache shiro的架包除了除了基本的以外,我们还需要shiro-web和shiro-spring的的架包,下面是所需要的所有shiro架包,至于其他的架包,像缓存的架包,Spring和SpringMVC的架包还是和我们以前使用的架包一样的。
org.apache.shiro
shiro-core
1.2.3
org.apache.shiro
shiro-ehcache
1.2.3
org.apache.shiro
shiro-web
1.2.3
org.apache.shiro
shiro-spring
1.2.3
所有的架包都搞清楚了以后,我们就可以开始正式搭建了,在myeclise中创建一个maven项目,将需要的架包信息依赖全部放入。下面就分步骤来创建
1.首先创建spring的配置文件,位置都在在resource中,配置文件是spring-context.xml,创建Apache Shiro的配置文件,名字是spring-context-shiro.xml,还有一个配置文件是springmvc的,配置文件是spring-mvc,这样起名是有原因的,因为这样我们就可以在web.xml中设置配置文件的时候,直接使用通配符了:
contextConfigLocation
classpath*:/spring-context-*.xml
org.springframework.web.context.ContextLoaderListener
这样就可以扫描到两个配置文件了,又不会扫描到我们的spring-mvc.xml了,
2除了在web.xml中设置这个以外,我们还需要设置spring-mvc的位置:
springServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springServlet
/
3.在web.xml中配置shiroFilter:
shiroFilter
org.springframework.web.filter.DelegatingFilterProxy
shiroFilter
/*
注意,这个shiroFilter名称,后面的配置还需要使用到,所以要注意咯。
4,因为shiro的session是自己实现的,所以我们还需要一个缓存框架,所以在spring的配置文件一定要注意配置哦,
spring的其他的配置,该怎样还是这样,我们的重点是配置spring-context-shiro.xml:先把配置的贴出来,然后讲一下这几个配置的意义:
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"default-lazy-init="true">
Shiro Configuration
/static/** = anon
/userfiles/** = anon
${adminPath}/cas = cas
${adminPath}/login = authc
${adminPath}/logout = logout
${adminPath}/** = user
ecurityManager:是shiro最重要的一个对象,授权和验证都是由它来做的,下面就一一的来讲他的依赖类,
一:realm:域,Shiro从从Realm获取安全数据(如用户、角色、权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法;也需要从Realm得到用户相应的角色/权限进行验证用户是否能进行操作;可以把Realm看成DataSource,即安全数据源。下对于源代码,我就不细细的研究了,下面是我重写的realm,:
packagecom.yonyou.hotusm.module.sys.security;importjava.io.Serializable;importorg.apache.shiro.authc.AuthenticationException;importorg.apache.shiro.authc.AuthenticationInfo;importorg.apache.shiro.authc.AuthenticationToken;importorg.apache.shiro.authc.SimpleAuthenticationInfo;importorg.apache.shiro.authc.UsernamePasswordToken;importorg.apache.shiro.authz.AuthorizationInfo;importorg.apache.shiro.authz.SimpleAuthorizationInfo;importorg.apache.shiro.authz.UnauthenticatedException;importorg.apache.shiro.realm.AuthorizingRealm;importorg.apache.shiro.subject.PrincipalCollection;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importcom.yonyou.hotusm.module.sys.dao.UserDao;importcom.yonyou.hotusm.module.sys.entity.User;importcom.yonyou.hotusm.module.sys.util.UserUtils;
@Servicepublic class SystemAuthorizingRealm extendsAuthorizingRealm{
@AutowiredprivateUserDao userDao;
@OverrideprotectedAuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
SimpleAuthorizationInfo info=newSimpleAuthorizationInfo();
info.addStringPermission("sys:manager");
info.addStringPermission("user");
System.out.println("开始授权");returninfo;
}
@OverrideprotectedAuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token)throwsAuthenticationException {
UsernamePasswordToken upToken=(UsernamePasswordToken) token;
String username=upToken.getUsername();
String password=newString(upToken.getPassword());
User user=newUser();
user.setLoginName(username);
user=userDao.get(user);
System.out.println("===========");if(user!=null){if(user.getPassword().equals(password)){return newSimpleAuthenticationInfo(username,password,getName());
}
}throw newUnauthenticatedException();
}public static class Principal implementsSerializable {private static final long serialVersionUID = 1L;private String id; //编号
private String loginName; //登录名
private String name; //姓名
publicPrincipal(User user) {this.id =user.getId();this.loginName =user.getLoginName();this.name =user.getName();
}publicString getId() {returnid;
}publicString getLoginName() {returnloginName;
}publicString getName() {returnname;
}/*** 获取SESSIONID*/
publicString getSessionid() {try{return(String) UserUtils.getSession().getId();
}catch(Exception e) {return "&#