AuthService.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package cn.com.qmth.print.manage.service;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.apache.commons.lang3.RandomStringUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import cn.com.qmth.print.manage.entity.UserEntity;
  7. import cn.com.qmth.print.manage.vo.UserVo;
  8. import com.qmth.boot.api.constant.ApiConstant;
  9. import com.qmth.boot.core.exception.StatusException;
  10. import com.qmth.boot.core.security.annotation.AuthorizationComponent;
  11. import com.qmth.boot.core.security.service.AuthorizationService;
  12. import com.qmth.boot.tools.signature.SignatureType;
  13. @AuthorizationComponent(prefix = ApiConstant.DEFAULT_URI_PREFIX)
  14. public class AuthService implements AuthorizationService<PmSession> {
  15. private Map<String, String> userMap = new HashMap<String, String>();
  16. @Autowired
  17. private UserService userService;
  18. public UserVo login(String loginName, String password) {
  19. UserEntity entity = userService.findByLoginName(loginName);
  20. if (entity == null) {
  21. throw new StatusException("用户不存在");
  22. }
  23. if (!entity.getPassword().equals(password)) {
  24. throw new StatusException("密码不正确");
  25. }
  26. if (!entity.isEnable()) {
  27. throw new StatusException("用户被禁用");
  28. }
  29. UserVo user = new UserVo(entity);
  30. String token = RandomStringUtils.randomAlphanumeric(32);
  31. userMap.put(user.buildKey(), token);
  32. user.setToken(token);
  33. return user;
  34. }
  35. public void logout(PmSession accessEntity) {
  36. userMap.remove(accessEntity.getIdentity());
  37. }
  38. @Override
  39. public PmSession findByIdentity(String identity, SignatureType type, String path) {
  40. return new PmSession(identity, userMap.get(identity));
  41. }
  42. @Override
  43. public boolean hasPermission(PmSession accessEntity, String path) {
  44. return true;
  45. }
  46. }