https://blog.csdn.net/cckevincyh/article/details/79629022
通过继承ModularRealmAuthenticator类,进行配置后接管了对realm的控制。
在重写的方法中,通过对realm的名字进行区分,使不同用户登录通过不同的realm。
可能有多的依赖,因为是直接从项目中copy来的,不用的删除即可
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.iot</groupId>
<artifactId>research</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>research</name>
<description>Industry-university-research platform</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--springboot配置处理器,加入后在配置文件中会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--devtool开发工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--数据库相关配置-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--thymeleaf相关配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- thymeleaf整合shiro标签 -->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
<!--shiro-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.iot.research.bean;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;
import java.util.ArrayList;
import java.util.Collection;
/**
* @Author 张满
* @Description
* * 当配置了多个Realm时,我们通常使用的认证器是shiro自带的org.apache.shiro.authc.pam.ModularRealmAuthenticator,其中决定使用的Realm的是doAuthenticate()方法
* * 自定义Authenticator
* * 注意,当需要分别定义处理学生和教师和管理员验证的Realm时,对应Realm的全类名应该包含字符串“Student”“Teacher”,或者“Admin”。
* * 并且,他们不能相互包含,例如,处理学生验证的Realm的全类名中不应该包含字符串"Admin"。
*
* @Date 2019/10/2 9:07
* @vsersion 1.0.0
**/
public class UserModularRealmAuthenticator extends ModularRealmAuthenticator {
@Override
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
// 判断getRealms()是否返回为空
assertRealmsConfigured();
// 强制转换回自定义的CustomizedToken
UserToken userToken = (UserToken) authenticationToken;
// 登录类型
LoginType loginType = userToken.getLoginType();
// 所有Realm
Collection<Realm> realms = getRealms();
// 登录类型对应的所有Realm
Collection<Realm> typeRealms = new ArrayList<>();
for (Realm realm : realms) {
//根据realm的类名和loginType对比来决定哪个realm起作用
if (realm.getName().contains(loginType.toString()))
typeRealms.add(realm);
}
// 判断是单Realm还是多Realm
if (typeRealms.size() == 1){
return doSingleRealmAuthentication(((ArrayList<Realm>) typeRealms).get(0), userToken);
}
else{
return doMultiRealmAuthentication(typeRealms, userToken);
}
}
}
package com.iot.research.bean;
/**
* @Author 张满
* @Description 登录类型枚举,这里的枚举要包含于新建的realm的类名中,比如 : Admin的枚举包含在AdminRealm的类名中
* @Date 2019/10/2 9:00
* @Param
* @return
**/
public enum LoginType {
Admin,Teacher
}
package com.iot.research.bean;
import lombok.Data;
import org.apache.shiro.authc.UsernamePasswordToken;
/**
* @Author 张满
* @Description 用于登录时判断用户类型,替代UsernamePasswordToken
* @Date 2019/10/2 9:04
* @vsersion 1.0.0
**/
@Data
public class UserToken extends UsernamePasswordToken {
//登录类型
private LoginType loginType;
public UserToken(final String username, final String password,LoginType loginType) {
super(username,password);
this.loginType = loginType;
}
}
package com.iot.research.config.realm;
import com.iot.research.bean.UserToken;
import com.iot.research.entity.Teacher;
import com.iot.research.service.TeacherService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationException;
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.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @Author 张满
* @Description 教师认证授权realm
* @Date 2019/10/2 9:17
* @vsersion 1.0.0
**/
public class TeacherRealm extends AuthorizingRealm {
@Autowired
private TeacherService teacherService;
private static final Logger log = LoggerFactory.getLogger(TeacherRealm.class);
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
log.info("开始teacher权限授权...");
if (principals == null) {
throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
}
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
if(principals.getPrimaryPrincipal() instanceof Teacher){
Teacher teacher = (Teacher) principals.getPrimaryPrincipal();
authorizationInfo.addRole("teacher");
log.info(teacher.getAccount()+"获得老师权限");
return authorizationInfo;
}
return authorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
log.info("开始老师登录认证...");
UserToken userToken = (UserToken)token;
String username = userToken.getUsername(); //获取用户名,默认和login.html中的username对应。
Teacher teacher = teacherService.findTeacherByAccount(username);
if (teacher == null) {
//没有返回登录用户名对应的SimpleAuthenticationInfo对象时,就会在LoginController中抛出UnknownAccountException异常
throw new UnknownAccountException("此老师不存在!");
}
//验证通过返回一个封装了用户信息的AuthenticationInfo实例即可。
return new SimpleAuthenticationInfo(teacher,teacher.getPassword(),ByteSource.Util.bytes(teacher.getAccount()),getName());
}
}
package com.iot.research.util;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
/**
* @Author 张满
* @Description shiro md5加密
* @Date 2019/9/24 10:58
* @vsersion 1.0.0
**/
public class ShiroMD5Utils {
/**
* @Author 张满
* @Description 使用md5将密码加密
* @Date 2019/9/24 11:36
* @Param [username, pwd]
* @return java.lang.String
**/
public static String MD5Pwd(String username, String pwd) {
String md5Pwd = new SimpleHash("MD5", pwd,
ByteSource.Util.bytes(username), 2).toHex();
return md5Pwd;
}
}
package com.iot.research.controller;
import com.iot.research.bean.LoginType;
import com.iot.research.bean.Result;
import com.iot.research.bean.UserToken;
import com.iot.research.entity.Teacher;
import com.iot.research.service.TeacherService;
import com.iot.research.util.ShiroMD5Utils;
import com.iot.research.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.Date;
/**
* @Author 张满
* @Description 老师controller
* @Date 2019/10/2 11:29
* @vsersion 1.0.0
**/
@Controller
@RequestMapping("/teacher")
public class TeacherController {
@Autowired
private TeacherService teacherService;
/**
* @Author 张满
* @Description 注册
* @Date 2019/10/2 11:44
* @Param [teacher]
* @return com.iot.research.bean.Result
**/
@GetMapping("/doRegister")
@ResponseBody
public Result doRegister(Teacher teacher){
String account = teacher.getAccount();
if(StringUtils.isNotEmpty(account.trim())){
//查询用户是否已经注册
Teacher haveOne = teacherService.findTeacherByAccount(account);
if(haveOne!=null){
return Result.error("该用户已经被注册");
}
//密码加密
String md5Pwd = ShiroMD5Utils.MD5Pwd(account, teacher.getPassword());
teacher.setPassword(md5Pwd);
//补充信息
teacher.setCreatetime(new Date());
//注册
int count = teacherService.insertTeacher(teacher);
if(count==1){
//注册成功
return Result.ok("注册成功");
}
return Result.error("注册失败,服务器异常!");
}
return Result.error("用户名中不能包含空格");
}
/**
* @Author 张满
* @Description 执行登录
* @Date 2019/10/2 11:38
* @Param [username, password, session]
* @return com.iot.research.bean.Result
**/
@RequestMapping("/doLogin")
@ResponseBody
public Result doLogin(@RequestParam(required = true) String username,
@RequestParam(required = true) String password,
HttpSession session){
//将用户名和密码封装到继承了UsernamePasswordToken的userToken
UserToken userToken = new UserToken(username, password, LoginType.Teacher);
Subject subject = SecurityUtils.getSubject();
try {
//执行登录
subject.login(userToken);
//将用户信息放入session
Teacher teacher = (Teacher)subject.getPrincipal();
session.setAttribute("user",teacher);
//返回成功信息
Result result = Result.ok("登录成功");
return result;
} catch (UnknownAccountException e) {
e.printStackTrace();
return Result.error("用户不存在或已被冻结!");
}catch (IncorrectCredentialsException e){
e.printStackTrace();
return Result.error("密码错误");
}catch (Exception e){
e.printStackTrace();
return Result.error("其他错误");
}
}
}