一、准备工作
在进行ssm集成Shiro之前,需要准备相关的数据库表结构。如:用户表,角色表,权限表,用户与角色关系表,角色与权限关系表。
并且准备好相关的实体类,以及Mapper和Mapper的映射xml文件,还有进行认证的自定义Realm。
二、application-Shrio.xml文件的相关配置
在此,将shiro的配置内容与Spring的配置内容分开进行配置。
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/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">
<!--声明凭证匹配器-->
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="md5"></property>
<property name="hashIterations" value="2"></property>
</bean>
<!--声明UserRealm-->
<bean id="userRealm" class="com.xsh.realm.UserRealm">
<!--注入凭证匹配器-->
<property name="credentialsMatcher" ref="credentialsMatcher"></property>
</bean>
<!--配置SecurityManager-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--注入Realm-->
<property name="realm" ref="userRealm"></property>
</bean>
<!--配置Shiro的过滤器-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!--注入安全管理器-->
<property name="securityManager" ref="securityManager"></property>
<!--配置未登录时的跳转页面,默认是跳转到webapp/login.jsp目录-->
<property name="loginUrl" value="/index.jsp"></property>
<!--注入未授权的跳转页面,当访问某个资源权限不够时跳转的页面-->
<property name="unauthorizedUrl" value="/unauthorizedUrl.jsp"></property>
<!--配置过滤器链-->
<property name="filterChainDefinitions">
<value>
<!--放行index.jsp页面-->
/index.jsp=anon
<!--放行跳转到登录页面的路径-->
/login/tologin*=anon
<!--放行登录的请求-->
/login/login*=anon
<!--设置登出的路劲-->
/login/logout*=logout
<!--设置除此之外的路径全部拦截-->
/**=authc
</value>
</property>
</bean>
</beans>
三、在web.xml文件中添加集成Shiro的配置
<!--配置Shiro的集成-->
<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>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>shiroFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<servlet-name>dispatcherServlet</servlet-name>
</filter-mapping>
四、在applicationContext.xml文件中引入applicartion-shiro.xml文件
五、使用Shiro完成认证和授权
1、补充完成UserRealm代码
package com.xsh.realm;
import com.xsh.pojo.User;
import com.xsh.service.PermissionService;
import com.xsh.service.RoleService;
import com.xsh.service.UserService;
import com.xsh.utils.ActivierUser;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
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.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class UserRealm extends AuthorizingRealm {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
@Autowired
private PermissionService permissionService;
public String getName(){
return this.getClass().getSimpleName();
}
/**
* 认证
* @param token
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = token.getPrincipal().toString();
User user = userService.findUserByUsername(username);
if(user!=null){
//查询角色
List<String> roles = roleService.findRolesByUserid(user.getUserid());
//查询权限
List<String> permissions = permissionService.findPermissionByUserid(user.getUserid());
//封装到ActiverUser中
ActivierUser activierUser=new ActivierUser(user,roles,permissions);
//使用Shiro的API进行认证
SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(activierUser,user.getPassword(),this.getName());
return info;
}else{
return null;
}
}
/**
* 授权
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
ActivierUser activierUser = (ActivierUser) principalCollection.getPrimaryPrincipal();
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
List<String> roles = activierUser.getRoles();
List<String> permissions = activierUser.getPermissions();
if(roles!=null && roles.size()>0){
info.addRoles(roles);
}
if(permissions!=null && permissions.size()>0){
info.addStringPermissions(permissions);
}
return info;
}
}
2、补充完成RoleService和PermissionService的代码
这两个Service需要实现的功能分别是通过userid查询Role名称和通过userid查询Permission的权限代码pcode。
代码展示以PermissionServiceImpl为例。
package com.xsh.service.impl;
import com.xsh.mapper.PermissionMapper;
import com.xsh.pojo.Permission;
import com.xsh.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {
@Autowired
private PermissionMapper permissionMapper;
@Override
public List<String> findPermissionByUserid(Integer userid) {
List<Permission> list = permissionMapper.findPermissionByUserid(userid);
List<String> permissions=new ArrayList<>();
for (Permission permission: list) {
permissions.add(permission.getPcode());
}
return permissions;
}
}
3、创建登录页面,页面通过表单提交信息的方式将username和password提交到后台进行认证。
4、书写LoginController完成数据处理
package com.xsh.controller;
import com.xsh.utils.ActivierUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/login")
public class LoginController {
@RequestMapping("/tologin")
public String tologin(){
return "login";
}
@RequestMapping("/login")
public String login(String username, String password, HttpSession session){
Subject subject = SecurityUtils.getSubject();
System.out.println(username+password);
UsernamePasswordToken token=new UsernamePasswordToken(username,password);
try{
subject.login(token);
System.out.println("yes...............");
ActivierUser activierUser = (ActivierUser) subject.getPrincipal();
session.setAttribute("user",activierUser.getUser());
return "redirect:/user/useradmin";
}catch (Exception e){
System.out.println("no!!!!!!!!!");
return "redirect:/user/userkill";
}
}
}
LoginController负责接收前台的username和password,将数据封装到subject中交由Shiro框架进行认证。
至此就已经完成了ssm集成Shiro并且进行登录认证的过程,接下来举例完成授权功能。
5、创建登录成功用户管理的页面。
在此简单做个示意,页面通过四个a标签表示对user表的crud功能。
<%--
Created by IntelliJ IDEA.
User: 夏世华
Date: 2020/3/6
Time: 17:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
<title>useradmin</title>
</head>
<body>
<h1>登录成功</h1>
<br>
<br>
<shiro:hasPermission name="user:query">
<h2><a href="#">用户查询</a></h2>
</shiro:hasPermission>
<shiro:hasPermission name="user:add">
<h2><a href="#">用户添加</a></h2>
</shiro:hasPermission>
<shiro:hasPermission name="user:update">
<h2><a href="#">用户修改</a></h2>
</shiro:hasPermission>
<shiro:hasPermission name="user:delete">
<h2><a href="#">用户删除</a></h2>
</shiro:hasPermission>
</body>
</html>
在此是通过在jsp页面中引入shiro的jstl表达式来完成权限控制,当用户不具备某个权限的时候,该操作选项菜单将不会显示。