shiro之 shiro整合ssm

时间:2021-01-24 16:36:57

1. 整合ssm并且实现用户登录和菜单权限。

2. 将shiro整合到ssm中

  a).添加shiro相关jar包

  b).在web.xml种添加shiro的配置

<!-- 配置shirofilter 通过代理来配置,对象由spring容器来创建的,但是交由servlet容器来管理 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<!-- 表示bean的生命周期有servlet来管理 -->
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<!--表示在spring容器中bean的id,如果不配置该属性,那么默认和该filter的name一致-->
<param-name>targetBeanName</param-name>
<param-value>shiroFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

  c)在src下添加 applicationContext-shiro.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop
="http://www.springframework.org/schema/aop"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:tx
="http://www.springframework.org/schema/tx"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 配置securityManager -->
<property name="securityManager" ref="securityManager"/>
<!-- 当访问需要认证的资源时,如果没有认证,那么将自动跳转到该url;
如果不配置该属性 默认情况下会到根路径下的login.jsp
-->
<property name="loginUrl" value="/login"></property>
<!-- 配置认证成功后 跳转到那个url上,通常不设置,如果不设置,那么默认认证成功后跳转上上一个url -->
<property name="successUrl" value="/index"></property>
<!-- 配置用户没有权限访问资源时 跳转的页面 -->
<property name="unauthorizedUrl" value="/refuse"/>
<!-- 配置shiro的过滤器链 -->
<property name="filterChainDefinitions">
<value>
/toLogin=anon
/login=authc
/logout=logout
/**=authc
</value>
</property>
</bean>
<!-- 配置securityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm"/>
</bean>
<bean id="userRealm" class="cn.wh.realm.UserRealm"/>
</beans>

d) 修改loginController中登陆方法

//登录
@RequestMapping("/login")
public ModelAndView login(HttpServletRequest request){
ModelAndView mv
=new ModelAndView("login");
String className
=(String)request.getAttribute("shiroLoginFailure");
if(UnknownAccountException.class.getName().equals(className)){
//抛出自定义异常
mv.addObject("msg", "用户名或密码错误!!");
}
else if(IncorrectCredentialsException.class.getName().equals(className)){
//抛出自定义异常
mv.addObject("msg", "用户名或密码错误!!");
}
else{
mv.addObject(
"msg", "系统异常!!");
}
return mv;
}

e) 添加自定义Realm:UserRealm

public class UserRealm extends AuthorizingRealm{
@Override
public String getName() {
return "userRealm";
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
String username
= token.getPrincipal().toString();
String pwd
="1111";
return new SimpleAuthenticationInfo(username, pwd,getName());
}
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
return null;
}
}

f)修改UserRealm实现身份认证

public class UserRealm extends AuthorizingRealm{
@Autowired
private UserService userService;
@Autowired
private PermissionService permissionService;
@Override
public String getName() {
return "userRealm";
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
String username
= token.getPrincipal().toString();
User user
= userService.findUserByName(username);
//设置该user的菜单
if(user!=null){
user.setMenus(permissionService.findByUserId(user.getId()));
}
return new SimpleAuthenticationInfo(user, user.getPwd(),getName());
}
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {

return null;
}
}

e)凭证匹配器配置

<!-- 配置自定义realm -->
<bean id="userRealm" class="cn.sxt.realm.UserRealm">
<property name="credentialsMatcher" ref="credentialsMatcher"/>
</bean>
<!-- 配置凭证匹配器 -->
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="md5"/>
<property name="hashIterations" value="2"/>
</bean>

userRealm要相应改变

//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
String username
= token.getPrincipal().toString();
User user
= userService.findUserByName(username);
//设置该user的菜单
if(user!=null){
user.setMenus(permissionService.findByUserId(user.getId()));
}
return new SimpleAuthenticationInfo(user, user.getPwd(),ByteSource.Util.bytes(user.getSalt()),getName());
}

logout配置,默认退出后跳转到跟路径下,如果需要改变则需从新配置logout过滤器,过滤器bean的id不能改变,只能为logout

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 配置securityManager -->
<property name="securityManager" ref="securityManager"/>
<!-- 当访问需要认证的资源时,如果没有认证,那么将自动跳转到该url;
如果不配置该属性 默认情况下会到根路径下的login.jsp
-->
<property name="loginUrl" value="/login"></property>
<!-- 配置认证成功后 跳转到那个url上,通常不设置,如果不设置,那么默认认证成功后跳转上上一个url -->
<property name="successUrl" value="/index"></property>
<!-- 配置用户没有权限访问资源时 跳转的页面 -->
<property name="unauthorizedUrl" value="/refuse"/>
<!-- 配置shiro的过滤器链
logout默认退出后跳转到根路径下,可以从新指定
-->
<property name="filterChainDefinitions">
<value>
/toLogin=anon
/login=authc
/logout=logout
/js/**=anon
/css/**=anon
/images/**=anon
/**=anon
</value>
</property>
</bean>
<!-- 配置logout过滤器 -->
<bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
<property name="redirectUrl" value="/toLogin"/>
</bean>

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

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 配置securityManager -->
<property name="securityManager" ref="securityManager"/>
<!-- 当访问需要认证的资源时,如果没有认证,那么将自动跳转到该url;
如果不配置该属性 默认情况下会到根路径下的login.jsp
-->
<property name="loginUrl" value="/login"></property>
<!-- 配置认证成功后 跳转到那个url上,通常不设置,如果不设置,那么默认认证成功后跳转上上一个url -->
<property name="successUrl" value="/index"></property>
<!-- 配置用户没有权限访问资源时 跳转的页面 -->
<property name="unauthorizedUrl" value="/refuse"/>
<!-- 配置shiro的过滤器链
logout默认退出后跳转到根路径下,可以从新指定
-->
<property name="filterChainDefinitions">
<value>
/toLogin=anon
/login=authc
/logout=logout
/js/**=anon
/css/**=anon
/images/**=anon
/**=anon
</value>
</property>
</bean>
<!-- 配置authc过滤器 -->
<bean id="authc" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">
<property name="usernameParam" value="name"/>
<property name="passwordParam" value="pwd"/>
</bean>

登陆页面的改变:

<form id="slick-login" action="login" method="post">
<label for="username">username</label><input type="text" name="name" class="placeholder" placeholder="用户名">
<label for="password">password</label><input type="password" name="pwd" class="placeholder" placeholder="密码">
<input type="submit" value="Log In">
</form>