Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * 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.zxy.commons.web.security; import javax.annotation.PostConstruct; import com.google.common.base.Preconditions; import com.google.common.hash.Hashing; import com.zxy.commons.web.constant.ShiroConstant; import org.apache.shiro.authc.*; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; 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; /** * ? * * <p> * <a href="AbstractCustomerShiroDbRealm.java"><i>View Source</i></a> * * @param <T> session class type * @author zhaoxunyong@qq.com * @version 1.0 * @since 1.0 */ //@Transactional(readOnly = true) public abstract class AbstractCustomerShiroDbRealm<T> extends AuthorizingRealm { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); /** * session * <p> * ????? <br /> * : ?session <br /> * : {@code org.apache.shiro.authz.AuthorizationInfo} * * @param username username * @param password password * @return ?session * @throws AuthenticationException AuthenticationException */ protected abstract T sessionCheck(String username, String password) throws AuthenticationException; /** * ??shiro * * @param simpleAuthorizationInfo simpleAuthorizationInfo * @param sessionObject sessionObject */ protected abstract void addRoleAndPermissions(SimpleAuthorizationInfo simpleAuthorizationInfo, T sessionObject); /** * ?,. */ // @Transactional(readOnly = false, rollbackFor = Exception.class) @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) { Preconditions.checkArgument(authcToken instanceof UsernamePasswordToken, "AuthenticationToken is not UsernamePasswordToken instance."); UsernamePasswordToken token = (UsernamePasswordToken) authcToken; // ??? String username = token.getUsername(); String password = String.valueOf(token.getPassword()); T sessionObject = sessionCheck(username, password); String decodePassword = Hashing.sha1().hashBytes(String.valueOf(token.getPassword()).getBytes()).toString(); return new SimpleAuthenticationInfo(sessionObject, decodePassword.toCharArray(), getName()); } /** * ?, ???. */ // @Transactional(readOnly = true) @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { @SuppressWarnings("unchecked") T sessionObject = (T) principals.getPrimaryPrincipal(); // ?? // if (!shiroUser.isHasFillData()) { // checkAndFillData(shiroUser); // } SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); // // Role??? // info.addRole(shiroUser.getEmpJob());// ???? // // Permission??? // info.addStringPermissions(shiroUser.getPermissions()); addRoleAndPermissions(info, sessionObject); return info; } /** * PasswordHash. */ @PostConstruct public void initCredentialsMatcher() { HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(ShiroConstant.HASH_ALGORITHM); matcher.setHashIterations(ShiroConstant.HASH_INTERATIONS); setCredentialsMatcher(matcher); } }