12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package com.qxgmat.util.shiro;
- import com.qxgmat.data.dao.entity.User;
- import com.qxgmat.service.UsersService;
- import org.apache.shiro.authc.*;
- 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.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import java.util.ArrayList;
- /**
- * Created by GaoJie on 2017/11/3.
- */
- public class UserRealm extends AuthorizingRealm {
- private static final Logger logger = LoggerFactory.getLogger(UserRealm.class);
- @Autowired
- private UsersService usersService;
- @Override
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
- String className = UserRealm.class.getName();
- boolean selected = false;
- for(String realmNames: principalCollection.getRealmNames()){
- if(realmNames.contains(className)) selected = true;
- }
- if(!selected) return null;
- ArrayList<String> roleAuthorization = new ArrayList<String>();
- SimpleAuthorizationInfo sa = new SimpleAuthorizationInfo();
- roleAuthorization.add("user");
- sa.addRoles(roleAuthorization);
- return sa;
- }
- @Override
- protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
- UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
- String areaMobile = token.getUsername();
- String password = new String(token.getPassword());
- String[] info = areaMobile.split(":");
- if (info.length<2){
- throw new UnknownAccountException("手机格式错误!");
- }
- if (info[0]==null || info[0].isEmpty()) {
- throw new UnknownAccountException("国际码不能为空!");
- }
- if (info[1]==null || info[1].isEmpty()) {
- throw new UnknownAccountException("手机不能为空!");
- }
- User user = usersService.getByMobile(info[0], info[1]);
- if (user == null || user.getId() <= 0) {
- throw new UnknownAccountException("用户不存在!");
- }
- // if(!usersService.equalsPassword(user, password)){
- // throw new IncorrectCredentialsException("用户名或密码错误!");
- // }
- return new SimpleAuthenticationInfo(user, password, getName());
- }
- }
|