shiro入门实例
简介: Shiro 是一个 Apache Incubator 项目,旨在简化身份验证和授权。是一个很不错的安全框架。
下面记录一下shiro和Spring整合的过程的一个小示例:
Web.xml配置
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml,classpath:spring-shiro.xml</param-value></context-param><!-- apache shiro权限 --><filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"><description>Shiro 配置</description><bean id="shiroFilter" ref="securityManager" /><property name="loginUrl" value="/login.jsp" /><property name="successUrl" value="/login.jsp" /><property name="unauthorizedUrl" value="/error/noperms.jsp" /><property name="filterChainDefinitions"><value>/login.jsp* = anon/login.do* = anon/index.jsp*= anon/error/noperms.jsp*= anon/*.jsp* = authc/*.do* = authc</value></property></bean><bean id="securityManager" ref="monitorRealm" /></bean><bean id="lifecycleBeanPostProcessor" /><!--自定义Realm 继承自AuthorizingRealm --><bean id="monitorRealm" /><property name="arguments" ref="securityManager" /></bean><!-- Enable Shiro Annotations for Spring-configured beans. Only run after --><!-- the lifecycleBeanProcessor has run: --><bean/><beanref="securityManager" /></bean></beans>
@Service("monitorRealm")public class MonitorRealm extends AuthorizingRealm {/* * @Autowired UserService userService; * * @Autowired RoleService roleService; * * @Autowired LoginLogService loginLogService; */public MonitorRealm() {super();}@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {/* 这里编写授权代码 */Set<String> roleNames = new HashSet<String>(); Set<String> permissions = new HashSet<String>(); roleNames.add("admin"); permissions.add("user.do?myjsp"); permissions.add("login.do?main"); permissions.add("login.do?logout");SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames); info.setStringPermissions(permissions);return info;}@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {/* 这里编写认证代码 */UsernamePasswordToken token = (UsernamePasswordToken) authcToken;//User user = securityApplication.findby(upToken.getUsername());User user = new User();user.setUsercode(token.getUsername());user.setUserName("admin");user.setPassword(EncryptUtils.encryptMD5("admin"));//if (user != null) {return new SimpleAuthenticationInfo(user.getUserName(),user.getPassword(), getName());}public void clearCachedAuthorizationInfo(String principal) {SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());clearCachedAuthorizationInfo(principals);}}@Controller@RequestMapping(value="user")public class UserController {/** * 跳转到myjsp页面 * * @return */@RequestMapping(params = "myjsp")public String home() {Subject currentUser = SecurityUtils.getSubject();if(currentUser.isPermitted("user.do?myjsp")){return "/my";}else{return "error/noperms";}}}Set<String> roleNames = new HashSet<String>(); Set<String> permissions = new HashSet<String>(); roleNames.add("admin"); permissions.add("user.do?myjsp"); permissions.add("login.do?main"); permissions.add("login.do?logout");SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames); info.setStringPermissions(permissions);return info;最后构造一个对象并把权限给它就OK拉。如果是数据库查出来的,直接我的字符串替成你查出来的就行了。这样在你的controller中根据权限返回到指定的页面。if(currentUser.isPermitted("user.do?myjsp")){return "/my";}else{return "error/noperms";}