Shiro+SSM整合,认证授权

   环境 : 

    Shiro+SSM整合基于你已经拥有一个可运行的SSM+Maven环境。

   主体 :

    1. 导入shiro相关jar包

    2. web.xml配置shiro的代理过滤器,shiro的入口,相当于建立起servlet与shiro的联系

    3. 配置shiro的xml文件

    4. 自定义 Realm,获取数据库安全数据

    5. controller层调用shiro的login方法验证用户信息

    步骤 :

    1. pom.xml导入相关jar包

    

        
		
			org.apache.shiro
			shiro-core
			1.2.1
		
		
			org.apache.shiro
			shiro-web
			1.2.1
		
		
			org.apache.shiro
			shiro-ehcache
			1.2.1
		
		
			org.apache.shiro
			shiro-spring
			1.2.1
		
		

        2. 配置web.xml

         这个地方只需要在web.xml中添加如下配置

	
	
	
		
		contextConfigLocation
		
			classpath:spring-mybatis.xml,
			classpath:spring-shiro.xml
		
	
	
	
	
		shiroFilter
		org.springframework.web.filter.DelegatingFilterProxy
	
	
		shiroFilter
		/*
	

        filter-name  默认是shiroFilter,要修改的话可以在这个地方配置,为了改个名字添加额外配置,个人感觉这个很鸡肋,不做介绍。

        3. 定制自己的shiro服务,我这里命名spring-shiro.xml

        要注意的是文件名和web.xml中配置的文件名保持一致。

        




	
	
		
		
		
	
	
	
	
		
		
		
		
            
                 
                 
            
        
	

	
	

	
	
	

	
	
		
		
		
		
		
		
		
			
				/logon.jsp = anon
				/user/logon = anon
				/index.jsp = authc
				/admin/** = roles[admin]
				/user/** = roles[user]
                /logout=logout
				/** = authc
			
		
	

 

      配置中介绍的比较详细,不涉及的知识点有多Realm配置及认证策略,以上的配置属于单个Realm的AtLeastOneSuccessfulStrategy认证策略(有一个成功就登录成功)。

      还有URL匹配优先级问题,简单描述就是从上到下依次匹配,以第一次匹配到的为准。这个很好理解,举个例子,/logon.jsp = anon 这个表达式的意思是根目录下的logon.jsp可以匿名访问,/** = authc 这个表达式表示所有的资源都需要认证过后才能访问,很明显/**包括/logon.jsp,而实际上我们访问logon.jsp是可以匿名访问的,原因就是URL匹配优先级以第一次匹配到的规则为准。

        4. 自定义 Realm

package com.hans.shiro;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
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.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.hans.entity.User;
import com.hans.service.UserService;

/**
 * @todo 自定义 Realm,查询数据库并返回正确的数据
 * @author Hans
 * @time 2018下午6:11:20
 *
 */
public class UserRealm extends AuthorizingRealm{
	
	@Autowired
	private UserService userSer;

	@Autowired
	private User us;

	/**
	 * @see 授权,在配有缓存的情况下,只加载一次。
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
		//当前登录用户,账号
		String userCode = principal.toString();
		System.out.println("当前登录用户:"+userCode);
		//获取角色信息
		List> roleList = new ArrayList>();
		roleList = userSer.findRoles(userCode);
		Set roles = new HashSet();
		
		if(roleList.size()>0){
			for(Map role : roleList){
				roles.add(String.valueOf(role.get("rcode")));
			}
		}else{
			System.out.println("当前用户没有角色!");
		}
		
		SimpleAuthorizationInfo info = null;
		info = new SimpleAuthorizationInfo(roles);
		return info;
	}
	
	/**
	 * @see 认证登录,查询数据库,如果该用户名正确,得到正确的数据,并返回正确的数据
	 * 		AuthenticationInfo的实现类SimpleAuthenticationInfo保存正确的用户信息
	 *    
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//1.将token转换为UsernamePasswordToken
		UsernamePasswordToken userToken = (UsernamePasswordToken)token;
		//2.获取token中的登录账户
		String userCode = userToken.getUsername();
		//3.查询数据库,是否存在指定的用户名和密码的用户(主键/账户/密码/账户状态/盐)
		us = null;
		us = userSer.findUserByUserCode(userCode);
		//4.1 如果没有查询到,抛出异常
		if( us == null ) {
			throw new UnknownAccountException("账户"+userCode+"不存在!");
		}
		if( us.getStatus() == 0){
			throw new LockedAccountException(us.getUsercode()+"被锁定!");
		}
		//4.2 如果查询到了,封装查询结果,
		Object principal = us.getUsercode();
		Object credentials = us.getPassword();
		String realmName = this.getName();
		String salt = us.getSalt();
		//获取盐,用于对密码在加密算法(MD5)的基础上二次加密ֵ
		ByteSource byteSalt = ByteSource.Util.bytes(salt);
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, credentials, byteSalt, realmName);
		//5. 返回给调用login(token)方法
		return info;
	}

}

            5. controller层验证用户信息

package com.hans.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hans.entity.User;
import com.hans.service.UserService;

/**
 * @todo 
 * @author Hans
 * @time 2018下午6:34:04
 *
 */
@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	UserService userSer;
	@Autowired
	User user;
	/**
	 * @todo 用户登录
	 * @since 获取当前用户,
	 * 判断用户是否已经认证登录,
	 * 用账号密码创建UsernamePasswordToken,
	 * 调用subject的login方法
	 * @param
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST,value = "logon")
	public String logon(@RequestParam("userCode")String userCode,@RequestParam("password")String password){
		//创建Subject实例对象
		Subject currentUser = SecurityUtils.getSubject();
		//判断当前用户是否已登录
		if(currentUser.isAuthenticated() == false){
			UsernamePasswordToken token = new UsernamePasswordToken(userCode,password);
			try {
				currentUser.login(token);
			} catch (AuthenticationException e) {
				e.getMessage();
				e.printStackTrace();
				System.out.println("登录失败");
				return "logon";
			}
		}
		return "index";
	}
}

      配置完成,到这里就可以实现简单的登录授权功能。

      下面看看效果,用户登录。

Shiro+SSM整合,认证授权_第1张图片

Shiro+SSM整合,认证授权_第2张图片

点击管理员资源,跳转拦截


点击用户资源,跳转




你可能感兴趣的:(Shiro)