ShiroDbRealm.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package com.key.common.plugs.security;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.UUID;
  23. import javax.servlet.http.HttpServletRequest;
  24. import com.key.common.base.entity.User;
  25. import com.key.common.base.service.AccountManager;
  26. import com.key.common.utils.security.DigestUtils;
  27. import org.apache.shiro.authc.AuthenticationException;
  28. import org.apache.shiro.authc.AuthenticationInfo;
  29. import org.apache.shiro.authc.AuthenticationToken;
  30. import org.apache.shiro.authc.SimpleAuthenticationInfo;
  31. import org.apache.shiro.authc.UsernamePasswordToken;
  32. import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
  33. import org.apache.shiro.authz.AuthorizationInfo;
  34. import org.apache.shiro.authz.SimpleAuthorizationInfo;
  35. import org.apache.shiro.realm.AuthorizingRealm;
  36. import org.apache.shiro.subject.PrincipalCollection;
  37. import org.apache.shiro.subject.SimplePrincipalCollection;
  38. import org.springframework.beans.factory.annotation.Autowired;
  39. import com.key.common.utils.web.Struts2Utils;
  40. public class ShiroDbRealm extends AuthorizingRealm {
  41. @Autowired
  42. protected AccountManager accountManager;
  43. public ShiroDbRealm() {
  44. setCredentialsMatcher(new HashedCredentialsMatcher("SHA-1"));
  45. }
  46. /**
  47. * 认证回调函数,登录时调用.
  48. */
  49. @Override
  50. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
  51. UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
  52. // User user = accountManager.findUserByLoginName(token.getUsername());
  53. //根据loginToken 看能不查到当前token token有效期就1分钟
  54. String tokenPassword=new String(token.getPassword());
  55. User user = accountManager.findUserByLoginNameOrEmail(token.getUsername());
  56. //user.getStandardLock()==1
  57. if (user != null && user.getStatus().intValue()!=0 && !user.getLoginName().endsWith("@chacuo.net")) {
  58. return new SimpleAuthenticationInfo(user.getLoginName(), user.getShaPassword() , getName());
  59. } else {
  60. return null;
  61. }
  62. }
  63. /**
  64. * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
  65. */
  66. @Override
  67. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  68. String username = (String) principals.fromRealm(getName()).iterator().next();
  69. // User user = accountManager.findUserByLoginName(username);
  70. User user = accountManager.findUserByLoginNameOrEmail(username);
  71. if (user != null && "1".equals(user.getId())) {
  72. SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
  73. info.addRole("admin");
  74. return info;
  75. } else {
  76. return null;
  77. }
  78. }
  79. /**
  80. * 更新用户授权信息缓存.
  81. */
  82. public void clearCachedAuthorizationInfo(String principal) {
  83. SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
  84. clearCachedAuthorizationInfo(principals);
  85. }
  86. }