Java tutorial
/** The Apache License 2.0 Copyright (c) 2016 Ramostear 2016818 ?2:42:07 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.seelecloud.cms.shiro.realm; import java.util.HashSet; import java.util.Set; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.LockedAccountException; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authz.AuthorizationException; 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.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.seelecloud.cms.entity.Role; import com.seelecloud.cms.entity.Manager; import com.seelecloud.cms.service.ManagerService; import com.seelecloud.cms.service.RoleService; /** * @description: * @author: vabo * @version: * @Datetime:20161018 * @Email: */ @Component("managerRealm") public class ManagerRealm extends AuthorizingRealm { @Autowired private ManagerService managerService; @Autowired private RoleService roleService; /** * ??? */ protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { System.out.println("doAuthz"); if (principals == null) { throw new AuthorizationException("Principal?"); } String managerName = (String) principals.getPrimaryPrincipal(); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); // ????. // info.setStringPermissions(???); // info.setRoles(?); // authorizationInfo.setStringPermissions(managerService.findPermissions(managerName)); if (managerName != null) { System.out.println("\nmanager name:" + managerName); } Manager manager = this.managerService.findByName(managerName); int roleId = manager.getRoleId(); Role role = roleService.findById(roleId); if (role == null) { System.out.println("\nrole is null"); } else { System.out.println("\nrole name:" + role.getRoleName()); } Set<String> roleNameSet = new HashSet<String>(); roleNameSet.add(role.getRoleName()); authorizationInfo.setRoles(roleNameSet); return authorizationInfo; } /** * ? */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.out.println("doAuthc"); String managerName = (String) token.getPrincipal(); if (managerName != null) { System.out.println("\nmanager name:" + managerName); } Manager manager = managerService.findByName(managerName); if (manager == null) { throw new UnknownAccountException();// ?? } else { System.out.println("\nmanager id:" + manager.getId() + manager.getStatus()); } if (Boolean.FALSE.equals(manager.getStatus())) { throw new LockedAccountException(); // 0??, ??? } // AuthenticatingRealmCredentialsMatcher???? SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(manager.getManagerName(), // ?? manager.getPassword(), // ? // ByteSource.Util.bytes(manager.getCredentialsSalt()),//salt=username+salt getName() // realm name ); return authenticationInfo; } }