Springmvc+Shiro+zTree 实战(三):spring整合shiro

推荐shiro教程:https://www.w3cschool.cn/shiro/

 

Spring整合shiro步骤解读:

一:导入shiro相关依赖

二:web.xml中配置shiro过滤器

三:编写自定义的DbRealm,进行认证和授权

四:spring整合shiro配置文件:applicationContext-shiro.xml

五:shiro缓存文件:ehcache-shiro.xml

六:spring容器配置加载applicationContext-shiro.xml文件

 

一:导入shiro相关依赖


	org.apache.shiro
	shiro-core
	1.4.0


	org.apache.shiro
	shiro-ehcache
	1.4.0


	org.apache.shiro
	shiro-web
	1.4.0


	org.apache.shiro
	shiro-spring
	1.4.0

二:web.xml中配置shiroFilter



	shiroFilter
	org.springframework.web.filter.DelegatingFilterProxy


	shiroFilter
	/*

 

三:编写自定义的DbRealm,实现用户的认证和授权

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.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.mote.pojo.Role;
import com.mote.pojo.User;
import com.mote.service.RoleService;
import com.mote.service.UserService;

public class ShiroDbRealm extends AuthorizingRealm {

	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;

	/**
	 * 认证
	 */
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken authcToken) throws AuthenticationException {

		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;//转为UsernamePasswordToken
		
		String userName = token.getUsername();//获取用户名
		String password = new String(token.getPassword());//获取密码
		
		User user = userService.getUserByNamePwd(userName, password);//通過用户名和密码获取用户
		if (user == null)
			return null;

		// 身份认证验证成功,返回一个AuthenticationInfo实现
		return new SimpleAuthenticationInfo(user.getUserName(),
				password, getName());

	}

	/**
	 * 授权
	 */
	protected AuthorizationInfo doGetAuthorizationInfo(
			PrincipalCollection principals) {

		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

		String userName = (String) getAvailablePrincipal(principals); // 获取用户名

		Role role = roleService.getRolesByUserName(userName);// 获取用户角色

		if (role == null)
			return null;

		info.addRole(role.getRoleName()); // 添加角色

		List perms = roleService.getPerm(role.getId());// 获取角色对应的权限
		info.addStringPermissions(perms); // 添加权限

		return info;
	}

}

 

对应的sql语句:





四:创建spring整合shiro的配置文件:applicationContext-shiro.xml




	
	

	
	
		
	

	
	
		
		
	

	
	
		
		
	

	
	
		
		
		
		
		
		
		
		
			
				
				
			
		
		
			
			
				/css/** = anon
				/img/** = anon
				/js/** = anon
				/login = anon
				/loginOut = logout  
				/**/**=user
			
		
	


	
	

	
	
		
	
	
	
		
	

关于权限管理filterChainDefinitions过滤器配置可以参考:filterChainDefinitions配置

 

五:创建ehcache缓存文件:shiro-ehcache.xml



    

六:spring容器加载applicationContext-shiro.xml文件


    contextConfigLocation
    classpath:applicationContext*.xml

 

上一篇:系统的角色管理AND用户管理

下一篇:系统登录逻辑AND授权

你可能感兴趣的:(Springmvc+Shiro+zTree 实战(三):spring整合shiro)