一.凭证匹配器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>