1、添加依赖包
org.apache.shiro
shiro-core
1.4.0
org.apache.shiro
shiro-web
1.4.0
org.apache.shiro
shiro-spring
1.4.0
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 permissions = new ArrayList<>();
Set roles = user.getRoles();
if(roles.size()>0) {
for(Role role : roles) {
Set 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";
}
}