Java tutorial
/* * Copyright 2014-2015 snakerflow.com * * * * Licensed 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.earl.carnet.security.shiro; import java.util.List; import javax.annotation.PostConstruct; import javax.annotation.Resource; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.shiro.authc.AccountException; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; 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.Value; import com.earl.carnet.dao.UserDao; import com.earl.carnet.domain.sercurity.user.User; /** * shiro?? * @author yuqs * @since 0.1 */ public class ShiroAuthorizingRealm extends AuthorizingRealm { private static Logger log = LogManager.getLogger(ShiroAuthorizingRealm.class); @Resource UserDao userDao; @Value("#{public[hashIterations]}") private int hashIterations; // @Resource // RetryLimitCredentialsMatcher matcher; /** * ?? */ public ShiroAuthorizingRealm() {//? super(); // setAuthenticationTokenClass(UsernamePasswordToken.class); // // //TODO ???,?? // HashedCredentialsMatcher matcher = new HashedCredentialsMatcher("SHA-1");//? // matcher.setHashIterations(hashIterations); // setCredentialsMatcher(matcher); } @PostConstruct public void init2() { setAuthenticationTokenClass(UsernamePasswordToken.class); //TODO ???,?? HashedCredentialsMatcher matcher = new HashedCredentialsMatcher("SHA-1");//? matcher.setHashIterations(hashIterations); setCredentialsMatcher(matcher); } /** * ????????? */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken upToken = (UsernamePasswordToken) token; String username = upToken.getUsername(); if (username == null) { log.warn("???"); throw new AccountException("???"); } User user = null; try { user = userDao.findOneByLoginId(username); } catch (Exception ex) { ex.printStackTrace(); log.warn("?\n" + ex.getMessage()); } if (user == null) { log.warn("?"); throw new UnknownAccountException("?"); } log.info("?" + username + "?"); ShiroPrincipal subject = new ShiroPrincipal(user); List<String> authorities = userDao.findPrivilegeCode(user.getId()); List<String> rolelist = userDao.findRoleName(user.getId()); subject.setAuthorities(authorities); subject.setRoles(rolelist); subject.setAuthorized(true); return new SimpleAuthenticationInfo(subject, user.getPassword(), getName()); } /** * ????????? */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); //???? ShiroPrincipal subject = (ShiroPrincipal) super.getAvailablePrincipal(principals); String username = subject.getUsername(); Long userId = subject.getId(); try { if (!subject.isAuthorized()) { //?????? List<String> authorities = userDao.findPrivilegeCode(userId); List<String> rolelist = userDao.findRoleName(userId); subject.setAuthorities(authorities); subject.setRoles(rolelist); subject.setAuthorized(true); log.info("?" + username + "???......"); log.info("?" + username + " " + subject.getRoles()); log.info("?" + username + " ??" + subject.getAuthorities()); } } catch (RuntimeException e) { throw new AuthorizationException("?" + username + "?"); } //??? info.addStringPermissions(subject.getAuthorities()); info.addRoles(subject.getRoles()); return info; } }