一、什么是Shiro
Apache Shiro 是 Java 的一个安全框架。功能强大,使用简单的Java安全框架,它为开发人员提供一个直观而全面的认证,授权,加密及会话管理的解决方案。
Shiro 包含 10 个内容:
1) Authentication:身份认证/登录,验证用户是不是拥有相应的身份。
2) Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限;即判断用户是否能做事情,常见的如:验证某个用户是否拥有某个角色。或者细粒度的验证某个用户对某个资源是否具有某个权限。
3) Session Manager:会话管理,即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中;会话可以是普通 JavaSE 环境的,也可以是如 Web 环境的。
4) Cryptography:加密,保护数据的安全性,如密码加密存储到数据库,而不是明文存储。
5) Web Support:Web支持,可以非常容易的集成到 web 环境。
6) Caching:缓存,比如用户登录后,其用户信息、拥有的角色/权限不必每次去查,这样可以提高效率。
7) Concurrency:shiro 支持多线程应用的并发验证,即如在一个线程中开启另一个线程,能把权限自动传播过去。
8) Testing:提供测试支持。
9) Run As:允许一个用户假装为另一个用户(如果他们允许)的身份进行访问。
10) Remember Me:记住我,这个是非常常见的功能,即一次登录后,下次再来的话不用登录了。
二、SpringBoot中集成Shiro
1、添加依赖包
<!-- shiro -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<!-- shiro -->
2、实现Realm需要继承AuthorizingRealm
其中两个对象长得很像,AuthenticationInfo与AuthorizationInfo ,前者用于认证登录,后者用于权限控制的。
public class AuthRealm extends AuthorizingRealm{
@Autowired
private LoginService loginservice;
//认证登录
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken utoken=(UsernamePasswordToken) token; //获取用户输入的token
String username = utoken.getUsername();
String password = new String((char[]) utoken.getCredentials());
char[] s = utoken.getPassword();
ImMftAcc user = loginservice.userLogin(username, password);
if(user!=null) { //认证成功会将用户信息放入到Session中,由Shiro来管理
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute("SessionUser", user);
session.setAttribute("SeesionAccount", user.getAccount());
return new SimpleAuthenticationInfo(user, user.getPwd(),this.getClass().getName());
}else {
throw new UnknownAccountException();//异常会在执行登录时捕获到
}
}
//权限
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
ImMftAcc user = (ImMftAcc) principal.fromRealm(this.getClass().getName()).iterator().next();//获取session中的用户
List<String> permissions = new ArrayList<>();
Set<Role> roles = user.getRoles();
if(roles.size()>0) {
for(Role role : roles) {
Set<Module> modules = role.getModules();
if(modules.size()>0) {
for(Module module : modules) {
permissions.add(module.getMname());
}
}
}
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addStringPermissions(permissions);
return info;
}
}
public class CredentialsMatcher extends SimpleCredentialsMatcher{
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
UsernamePasswordToken utoken=(UsernamePasswordToken) token;
//用户输入的密码
String inPassword = new String(utoken.getPassword());
//数据库查询的密码
String dbPassword=(String) info.getCredentials();
return this.equals(inPassword, dbPassword);
}
}
3、实现登录
@Api(value="用户登录接口API",tags="用户登录")
@RestController
@RequestMapping("/user")
public class UserLoginController {
@Autowired
private LoginService loginService;
@ApiOperation(value = "用户登录", notes = "用户登录")
@ApiImplicitParams({
@ApiImplicitParam(name = "UserInfo", value = "用户对象", required = true, dataType = "UserInfo"),
})
@RequestMapping(value="/login",method=RequestMethod.POST)
public String loginUser(@RequestBody UserInfo userInfo) {
String accNo = userInfo.getAccount().trim();
String pwd = userInfo.getPwd().trim();
if(StringUtils.isEmpty(accNo)||StringUtils.isEmpty(pwd)) {
return "-2";
}
UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(accNo, pwd);
Subject subject = SecurityUtils.getSubject();
try {
subject.login(usernamePasswordToken);
UserInfo user = (UserInfo) subject.getPrincipal();
return "0";
} catch(Exception e) {
return "-1";
}
}
@ApiOperation(value = "注销登录", notes = "注销登录")
@RequestMapping(value="/logout",method=RequestMethod.GET)
public String logout(RedirectAttributes redirectAttributes ){
SecurityUtils.getSubject().logout();
return "0";
}
}