Shiro-----整合ssm核心代码

1.applicationContext-shiro.xml

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd" default-autowire="byName">
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<!-- 配置securityManager -->
		<property name="securityManager" ref="securityManager"></property>
		<!-- 当访问需要认证的资源时,如果没有认证,那么将自动跳转到该url;
		             如果不配置该属性,默认情况下会到根路径下的login.jsp -->
		<property name="loginUrl" value="/login"></property>
		<!-- 配置成功后,跳转到该url上,通常不设置,
		           如果不设置,那么默认认证成功后跳转到上一个url -->
		<property name="successUrl" value="/index"></property>
		<!-- 配置用户没有权限访问资源时跳转的页面 -->
		<property name="unauthorizedUrl" value="/refuse"></property>
		<!-- 配置shiro的过滤器链
			logout默认退出后跳转到根路径下,可以重新指定
		 -->
		<property name="filterChainDefinitions">
			<value>
				/toLogin=anon
				/login=authc
				/logout=logout
				/js/**=anon
				/css/**=anon			
				/images/**=anon	
				/index=user
				/**=authc		
			</value>
		</property>
	</bean>
	<!-- 配置authc过滤器 -->
	<bean id="authc" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">
		<property name="usernameParam" value="name"></property>
		<property name="passwordParam" value="pwd"></property>
		<property name="rememberMeParam" value="rememberMe"></property>
	</bean>
	<!-- 配置logout过滤器 -->
	<bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
		<property name="redirectUrl" value="/toLogin"></property>
	</bean>
	<!-- 配置securityManager -->
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="userRealm"></property>
		<property name="cacheManager" ref="cacheManager"></property>
		<property name="sessionManager" ref="sessionManager"></property>
		<property name="rememberMeManager" ref="rememberMeManager"></property>
	</bean>
	<!-- 记住我 -->
	<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
		<property name="cookie" ref="remenberMeCookie"></property>
	</bean>
	<!-- 记住我cookie -->
	<bean id="remenberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
		<!-- 设置cookie存活时间 -->
		<property name="maxAge" value="604800"></property>
		<!-- 设置cookie的名称 -->
		<property name="name" value="rememberMe"></property>
	</bean>
	<!-- 配置会话session管理器 -->
	<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
		<!-- 单位是毫秒 -->
		<property name="globalSessionTimeout" value="300000"></property>
		<!-- 删除无效session -->
		<property name="deleteInvalidSessions" value="true"></property>
	</bean>
	<!-- 配置缓存管理器 -->
	<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
		<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"></property>
	</bean>
	<!-- 配置自定义realm -->
	<bean id="userRealm" class="com.kennosaur.realm.UserRealm">
		<property name="credentialsMatcher" ref="credentialsMatcher"></property>
	</bean>
	<!-- 配置凭证匹配器 -->
	<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
		<property name="hashAlgorithmName" value="md5"></property>
		<property name="hashIterations" value="2"></property>
	</bean>
</beans>
    

2.springmvc.xml

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 扫描注解,只扫描controller包 -->
	<context:component-scan base-package="com.kennosaur.controller"></context:component-scan>
	<!-- 注解驱动,注册HandlerMapping和HandlerAdapter -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 设置静态资源 -->
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
	<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
	<mvc:resources location="/files/" mapping="/files/**"></mvc:resources>
	<!-- Multipart解析器     文件上传 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
	<!-- 开启aop代理 -->
	<aop:config proxy-target-class="true"></aop:config>
	<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
		<property name="securityManager" ref="securityManager"></property>
	</bean>
	<!-- 异常处理 -->
	<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<!-- key是异常的完全限定名  值是视图名 -->
				<!-- 认证异常 -->
				<prop key="org.apache.shiro.authz.UnauthenticatedException">login</prop>
				<!-- 授权异常 -->
				<prop key="org.apache.shiro.authz.UnauthorizedException">refuse</prop>
				
			</props>
		</property>
	</bean>
</beans>

3.UserRealm.java

package com.kennosaur.realm;

import java.util.List;

import org.apache.catalina.User;
import org.apache.shiro.SecurityUtils;
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.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.kennosaur.pojo.Users;
import com.kennosaur.service.UsersService;

import sun.net.ftp.FtpDirEntry.Permission;

public class UserRealm extends AuthorizingRealm{
	@Autowired
	private UsersService usersService;
	@Autowired
	private PermissionService permissionService;
	
	@Override
	public String getName() {
		return "userRealm";
	}
	//====认证=====5dbf2d01bf694d7c218c1ea456c51241
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		System.out.println("=======认证=======");
		String username = token.getPrincipal().toString();
		User user = usersService.findUserByName(username);
		//设置该user的菜单
		if (user!=null) {
			user.setMenus(permissionService.findMenuByUserId(user.getId()));
		}
		return new SimpleAuthenticationInfo(user, user.getPassword(),ByteSource.Util.bytes(user.getSalt()), getName());
	}
	//=====授权=======
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		//获取身份信息---------该身份信息在认证时已设置
		Users user = (Users)principals.getPrimaryPrincipal();
		if (user==null) {
			return null;
		}
		List<Permission> permissions = permissionService.findPermissionByUserId(user.getId());
		if (permissions==null||permissions.size()==0) {
			return null;
		}
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		for (Permission p : permissions) {
			info.addStringPermission(p.getPercode());
		}		
		return info;
	}
	
//	@Override
//	protected void clearCache(PrincipalCollection principals) {
//		Subject subject = SecurityUtils.getSubject();
//		super.clearCache(subject.getPrincipals());
//	}
	//清理缓存的方法
	public void clearCache() {
		Subject subject = SecurityUtils.getSubject();
		super.clearCache(subject.getPrincipals());
	}

}

4.login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="login" method="post">
		用户名: <input type="text" name="username"/>
		密码: <input type="password" name="password"/>
		<input type="checkbox" name="rememberMe">记住我
		<input type="submit" value="登录"/>
	</form>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值