Shiro-----整合ssm(凭证匹配器credentialsMatcher&&退出功能&&登录参数改变)

一.凭证匹配器credentialsMatcher

1.修改applicationContext-shiro.xml配置文件----------(注意实体类需要实现序列化implements Serializable)

<?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的过滤器链 -->
		<property name="filterChainDefinitions">
			<value>
				/toLogin=anon
				/login=authc
				/logout=logout
				/js/**=anon
				/css/**=anon			
				/images/**=anon	
				/**=authc		
			</value>
		</property>
	</bean>
	<!-- 配置securityManager -->
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="userRealm"></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.修改UserRealm.java中的doGetAuthenticationInfo方法(把盐值加进去)

	//====认证=====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.findByUserId(user.getId()));
		}
		return new SimpleAuthenticationInfo(user, user.getPassword(),ByteSource.Util.bytes(user.getSalt()), getName());
	}

二.退出功能--------默认退出后跳转到根路径下;配置LogoutFilter类中的redirectUrl属性

		<!-- 配置shiro的过滤器链
			logout默认退出后跳转到根路径下,可以重新指定
		 -->
		<property name="filterChainDefinitions">
			<value>
				/toLogin=anon
				/login=authc
				/logout=logout
				/js/**=anon
				/css/**=anon			
				/images/**=anon	
				/**=authc		
			</value>
		</property>
	</bean>
	<!-- 配置logout过滤器 -->
	<bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
		<property name="redirectUrl" value="/toLogin"></property>
	</bean>

三.登录参数改变----------修改FormAuthenticationFilter类中的usernameParam和passwordParam属性

改变登录时的表单域名称,需要重新配置authc过滤器

<!-- 配置shiro的过滤器链
			logout默认退出后跳转到根路径下,可以重新指定
		 -->
		<property name="filterChainDefinitions">
			<value>
				/toLogin=anon
				/login=authc
				/logout=logout
				/js/**=anon
				/css/**=anon			
				/images/**=anon	
				/**=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>
	</bean>

登录表单的name对应修改

	<form action="index" method="post">
		用户名: <input type="text" name="name"/>
		密码: <input type="password" name="pwd"/>
		<input type="submit" value="登录"/>
	</form>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值