SSM项目整合Shiro进行身份验证

一、简介

身份验证指的是系统一般需要提供一些标识信息来表明登录者的身份,如提供用户名和密码,或手机号和验证码来证明。在 Shiro 中,用户需要提供principals(身份)和 credentials(证明)给 Shiro来验证用户身份。

  • principals:身份即为主体的标识属性,它可以是任何属性,如用户名、 邮箱,手机号等,必须保证唯一。一个主体可以有多个principals,但只有一个 Primary principals,一般是用户名/邮箱/手机号。
  • credentials:证明/凭证,即只有主体知道的安全值,如密码/数字证 书等。

最常见的 principals 和 credentials 组合就是用户名和密码了。

二、流程

身份验证的具体流程,如下图所示:
在这里插入图片描述
下面具体说明图中的五个步骤,如下:

  1. 首先调用 Subject.login(token) 进行登录,其会自动委托给SecurityManager,调用之前必须通过 SecurityUtils.setSecurityManager() 设置;
  2. SecurityManager 负责真正的身份验证逻辑;它会委托给Authenticator 进行身份验证;
  3. Authenticator 才是真正的身份验证者,Shiro API 中核心的身份认证入口点,此处可以自定义插入自己的实现;
  4. Authenticator 可能会委托给相应的 AuthenticationStrategy 进 行多 Realm 身份验证,默认 ModularRealmAuthenticator 会调用AuthenticationStrategy 进行多 Realm 身份验证;
  5. Authenticator 会把相应的 token 传入 Realm,从 Realm 获取身份验证信息,如果没有返回或者抛出异常表示身份验证失败了。此处可以配置多个Realm,将按照相应的顺序及策略进行访问。

使用

1.环境配置

下面基于之前这篇文章里创建的SSM项目为基础来整合Shiro:https://blog.csdn.net/huangjhai/article/details/104068993

1.pom.xml上添加以下依赖:

        <!-- shiro包 -->     
		<dependency>
		    <groupId>org.apache.shiro</groupId>
		    <artifactId>shiro-all</artifactId>
		    <version>1.3.2</version>
		</dependency>
		<!-- Ehcache包 -->
		<dependency>
		    <groupId>net.sf.ehcache</groupId>
		    <artifactId>ehcache-core</artifactId>
		    <version>2.4.3</version>
		</dependency>

2.springmvc.xml添加shiro.filter配置,如下:

    <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>/*</url-pattern>
    </filter-mapping>

3.applicationContext.xml配置,:

   <!--   1. 配置 SecurityManager!-->     
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>     
        <property name="realm" ref="jdbcRealm" />  
        
    </bean>
    <!--  
    2. 配置 CacheManager. 
    2.1 需要加入 ehcache 的 jar 包及配置文件. 
    -->     
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> 
    </bean>
    <!-- 
    	3. 配置 Realm 
    	3.1 直接配置实现了 org.apache.shiro.realm.Realm 接口的 bean
    -->     
    <bean id="jdbcRealm" class="com.learn.shiro.realms.ShiroRealm">
        <property name="credentialsMatcher">
         <!-- 凭证匹配器的类型 -->
        <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
          <!--加密算法 -->
             <property name="hashAlgorithmName" value="MD5"></property>
         <!-- 加密次数 -->
             <property name="hashIterations" value="1024"></property>
        </bean>
        </property>    
    </bean>
    <!--  
    4. 配置 LifecycleBeanPostProcessor. 可以自定的来调用配置在 Spring IOC 容器中 shiro bean 的生命周期方法. 
    -->       
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
    <!--  
    5. 启用 IOC 容器中使用 shiro 的注解. 但必须在配置了 LifecycleBeanPostProcessor 之后才可以使用. 
    -->     
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
          depends-on="lifecycleBeanPostProcessor"/>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>
    <!--  
    6. 配置 ShiroFilter. 
    6.1 id 必须和 web.xml 文件中配置的 DelegatingFilterProxy 的 <filter-name> 一致.
    -->     
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login.jsp"/>
        <property name="successUrl" value="/list.jsp"/>
        <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
        <!--  
        	配置哪些页面需要受保护. 
        	以及访问这些页面需要的权限. 
        	1). anon 可以被匿名访问
        	2). authc 必须认证(即登录)后才可能访问的页面. 
        	3). logout 登出.
        -->
        <property name="filterChainDefinitions">
            <value>
                /login.jsp = anon
                /shiro/login=anon
                /shiro/logout=logout
                 /error=anon            
                # everything else requires authentication:
                /** = authc
            </value>
        </property>
    </bean> 

这里配置的cacheManager时出现了这样的错误:

Cannot convert value of type [org.apache.shiro.cache.ehcache.EhCacheManager] to required type [net.sf.ehcache.CacheManager] for property 'cacheManager': no matching editors or conversion strategy found

就是没法将[org.apache.shiro.cache.ehcache.EhCacheManager]转换成[net.sf.ehcache.CacheManager] 。这是由于我使用byname的方式自动注入bean。而引入的net.sf.ehcache包中有着同名的bean。这里可以改为byType方式注入或者bean换个id。

4.添加ehcache.xml,直接使用了Shiro整合Spring的示例中的,如下:

<ehcache>
    <diskStore path="java.io.tmpdir"/>  
    <cache name="authorizationCache"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>
    <cache name="authenticationCache"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>
    <cache name="shiro-activeSessionCache"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />
    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />
    <cache name="sampleCache2"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> 
</ehcache>

2.测试

1.创建pojo包并创建users类

public class Users {
	private int id;
	private String username;
	private String password;
	//省略getter和setting方法

2.创建mapper包并创建usersMapper接口和usersMapper.xml
usersMapper.java:

public interface UsersMapper {
    public Users selUser(String username);
}

usersMapper.xml.java:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  <mapper namespace="com.learn.mapper.UsersMapper">
   <select id="selUser" parameterType="string" resultType="com.learn.pojo.Users">
   select * from users where username=#{username}
   </select>
  </mapper>

3.创建service包并创建usersService接口和usersServiceImpl实现类
usersService.java:

public interface UsersService {
	 public Users selUser(String username);
}

usersServiceImpl.java:

@Service
public class UsersServiceImpl implements UsersService {
	@Resource
	private UsersMapper usersMapper;
	@Override
	public Users selUser(String username) {
		// TODO Auto-generated method stub
		return usersMapper.selUser(username);
	}
}

4.创建controller并创建LoginController类

@Controller
@RequestMapping("/shiro")
public class LoginController {
	@RequestMapping(value = "login",method = RequestMethod.POST)
	public String checkLogin(@RequestParam("username") String username,@RequestParam("password") String password) {		
	      Subject currentUser = SecurityUtils.getSubject();
	      if (!currentUser.isAuthenticated()) {
	        	// 把用户名和密码封装为 UsernamePasswordToken 对象
	            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
	            System.out.println(username+": "+password);
	            try {
	            	// 执行登录. 
	                currentUser.login(token);
	            } 
	            // 所有认证时异常的父类. 
	            catch (AuthenticationException ae) {
	                System.out.println("出现异常:——————->"+ae.getMessage());
	                return "error";
	            }
	        }
		return "list";
	}
}

5.创建ShiroRealm类继承AuthenticatingRealm,如下:

public class ShiroRealm extends AuthenticatingRealm{
	@Autowired
	private UsersServiceImpl usersService;

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		System.out.println(token);
		//1.AutenticationToken转换为UsernamePasswordToken
		UsernamePasswordToken usernamePasswordToken=(UsernamePasswordToken) token;
		//2。从UsernamePasswordToken中取出username
		String username=usernamePasswordToken.getUsername();		
		//3.从数据库中查询username对应的用户记录
		Users users=usersService.selUser(username);			
		
		//4.若用户不存在则抛出UnknowAccountException异常
		if (users==null) {
			throw new UnknownAccountException("用户不存在");
		}
		//5.根据用户信息的情况,决定是否需要抛出其他的 AuthenticationException异常
		
		//6.根据用户的情况,来构建AuthenticationInfo对象返回
		Object principal=username;//认证的实体信息,可以是用户名或数据包对应的用户实体类对象
		Object credentials=users.getPassword();//密码
		String realmName=getName();//当前realm对象的name,调用父类的getName()方法即可
		ByteSource credentialsSalt=ByteSource.Util.bytes(principal);//MD5的盐值
		/*
		 * 这里模拟生成在数据库中保存的经过以用户名为盐值的MD5算法加密后的密码
		 */
		Object hashedCredentials=new SimpleHash("MD5", credentials, credentialsSalt, 1024);
		System.out.println("数据库中的密码:"+hashedCredentials);
		System.out.println("用户输入的经Shiro加密后的密码:"+new SimpleHash("MD5", token.getCredentials(), credentialsSalt, 1024));
		SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(principal, hashedCredentials,credentialsSalt, realmName);
		return info;
	}
}

创建表单页面后进行测试项目,浏览器访问:
在这里插入图片描述
可以看到当密码不正确时,会抛出异常:did not match the expected credentials.

当数据库的密码和输入的密码一致时,则会返回SimpleAuthenticationInfo对象,如下:
在这里插入图片描述

多Realm配置

1.首先在applicationContext.xml中配置,其中选择加密算法为SHA1。

    <!-- 配置第二个Realm -->
   <bean id="jdbcRealmTwo" class="com.learn.shiro.realms.ShiroRealmTwo">
        <property name="credentialsMatcher">
         <!-- 凭证匹配器的类型 -->
        <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
          <!--加密算法 -->
             <property name="hashAlgorithmName" value="SHA1"></property>
         <!-- 加密次数 -->
             <property name="hashIterations" value="1024"></property>
        </bean>
        </property>    
    </bean>

2.在对应配置中的class信息创建ShiroRealmTwo,这里直接复制 ShiroReal,添加用于显示提示的代码,如下:

public class ShiroRealmTwo extends AuthenticatingRealm{
	@Autowired
	private UsersServiceImpl usersService;

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		System.out.println(token);
		System.out.println("---------ShiroRealmTwo  was called!------------");
		//1.AutenticationToken转换为UsernamePasswordToken
		UsernamePasswordToken usernamePasswordToken=(UsernamePasswordToken) token;
		//2。从UsernamePasswordToken中取出username
		String username=usernamePasswordToken.getUsername();		
		//3.从数据库中查询username对应的用户记录
		Users users=usersService.selUser(username);			
		
		//4.若用户不存在则抛出UnknowAccountException异常
		if (users==null) {
			throw new UnknownAccountException("用户不存在");
		}
		//5.根据用户信息的情况,决定是否需要抛出其他的 AuthenticationException异常
		
		//6.根据用户的情况,来构建AuthenticationInfo对象返回
		Object principal=username;//认证的实体信息,可以是用户名或数据包对应的用户实体类对象
		Object credentials=users.getPassword();//密码
		String realmName=getName();//当前realm对象的name,调用父类的getName()方法即可
		ByteSource credentialsSalt=ByteSource.Util.bytes(principal);//MD5的盐值
		/*
		 * 这里模拟生成在数据库中保存的经过以用户名为盐值的MD5算法加密后的密码,
		 * 在保存时可以使用SimpleHash进行构造加密后的密码。
		 */
		Object hashedCredentials=new SimpleHash("MD5", credentials, credentialsSalt, 1024);
		System.out.println("数据库中的密码:"+hashedCredentials);
		System.out.println("用户输入的经Shiro加密后的密码:"+new SimpleHash("SHA1", token.getCredentials(), credentialsSalt, 1024));
		//SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(principal, credentials, realmName);
		//进行身份验证
		SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(principal, hashedCredentials,credentialsSalt, realmName);		
	return info;
		
	}
}

3.配置Authenticator及AuthenticationStrategy
Authenticator的职责是验证用户帐号,是Shiro API中身份验证核心的入口点。如果验证成功,将返回AuthenticationInfo验证信息;此信息中包含了身份及凭证;如果验证失败将抛出相应的AuthenticationException实现。

SecurityManager接口继承了Authenticator,另外还有一个ModularRealmAuthenticator实现,其委托给多个Realm进行验证,验证规则通过AuthenticationStrategy接口指定。

ModularRealmAuthenticator实现验证功能的是doAuthenticate方法,而realms是一个集合。当只用一个realm时调用doSingleRealmAuthentication方法,realm数量大于一时调用doMultiRealmAuthentication方法。doAuthenticate方法的源码如下:

    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
        assertRealmsConfigured();
        Collection<Realm> realms = getRealms();
        if (realms.size() == 1) {
            return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);
        } else {
            return doMultiRealmAuthentication(realms, authenticationToken);
        }
    }

这里直接配置ModularRealmAuthenticator的realms属性:

    <bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
       <property name="realms">
          <list>
                <ref bean="jdbcRealm"/>
                <ref bean="jdbcRealmTwo"/>
          </list>  
       </property>
    </bean>

对应的需要配置SecurityManager的authenticator属性,将authenticator配置给SecurityManager。

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>      
        <property name="authenticator" ref="authenticator"></property>
    </bean>

这样就完成了第二个Realm的配置。这样执行后ModularRealmAuthenticator就会调用这两个Realm。

启动项目,查看控制台信息:在这里插入图片描述
可以看到securityManager会按照realms指定的顺序进行身份认证。先动用ShiroRealm再调用ShiroRealmTwo,对应了配置文件中的顺序。而ShiroRealmTwo的密码不一致,仍然可以成功登录。这是因为ModularRealmAuthenticator默认使用的认证策略是AtLeastOneSuccessfulStrategy策略
部分源码如下,可以看到ModularRealmAuthenticator的无参构造方法自动配置了AtLeastOneSuccessfulStrategy:

    private AuthenticationStrategy authenticationStrategy;
    public ModularRealmAuthenticator() {
        this.authenticationStrategy = new AtLeastOneSuccessfulStrategy();
    }

Shiro默认提供的AuthenticationStrategy接口实现有:

  • FirstSuccessfulStrategy:只要有一个Realm验证成功即可,只返回第一个Realm身份验证成功的认证信息,其他的忽略;
  • AtLeastOneSuccessfulStrategy:只要有一个Realm验证成功即可,和FirstSuccessfulStrategy不同,返回所有Realm身份验证成功的认证信息;
  • AllSuccessfulStrategy:所有Realm验证成功才算成功,且返回所有Realm身份验证成功的认证信息,如果有一个失败就失败了。

4 修改认证策略
自行配置ModularRealmAuthenticator中的AuthenticationStrategy即可自定义认证策略。
如在authenticator中配置AuthenticationStrategy为AllSuccessfulStrategy,需要所有Realm验证成功才能通过验证。如下:

    <bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
       <property name="realms">
          <list>
                <ref bean="jdbcRealm"/>
                <ref bean="jdbcRealmTwo"/>
          </list>  
       </property>
       <property name="authenticationStrategy">
          <bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"></bean>
       </property>
    </bean>

此时进行登录则会抛异常:did not match the expected credentials。无法通过用户验证。

这篇文章将对这个项目的身份验证流程的源码分析一下:
Shiro的身份验证流程的源码分析

参考:
https://www.w3cschool.cn/shiro/xgj31if4.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值