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.tianxiaxinyong.web.security.shrio; import java.util.Calendar; import java.util.Date; 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.LockedAccountException; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UsernamePasswordToken; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import com.tianxiaxinyong.enterprise.common.DataException; import com.tianxiaxinyong.enterprise.common.ServiceException; import com.tianxiaxinyong.enterprise.member.domain.Member; import com.tianxiaxinyong.enterprise.member.service.MemberService; import com.tianxiaxinyong.web.constance.WebConstance; /** * shiro?? * @author yuqs * @since 0.1 */ public class ShiroAuthorizingRealm extends AuthorizingRealm { private static Logger log = LoggerFactory.getLogger(ShiroAuthorizingRealm.class); @Autowired private MemberService<Member> memberService; /** * ?? */ public ShiroAuthorizingRealm() { // super(); // setAuthenticationTokenClass(UsernamePasswordToken.class); // HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(WebConstance.HASH_ALGORITHM); // matcher.setHashIterations(WebConstance.HASH_INTERATIONS); // setCredentialsMatcher(matcher); } /** * ????????? */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); //???? ShiroPrincipal subject = (ShiroPrincipal) super.getAvailablePrincipal(principals); String username = subject.getUsername(); String userId = subject.getId(); try { if (!subject.isAuthorized()) { //?????? /*List<String> authorities = Member.dao.getAuthoritiesName(userId); List<String> rolelist = Member.dao.getRolesName(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; } /** * ????????? */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken upToken = (UsernamePasswordToken) token; String username = upToken.getUsername(); if (username == null) { log.warn("???"); throw new AccountException("???"); } Member member = null; try { member = memberService.findMemberByMemberName(username); } catch (Exception ex) { log.error("?ex={}", ex); } if (member == null) { log.warn("?"); throw new UnknownAccountException("?!"); } if (2 == member.getUserStatus()) { log.warn("?"); throw new UnknownAccountException("?!"); } if (3 == member.getUserStatus()) { log.warn("??"); throw new LockedAccountException("?!"); } //? Date lockedDate = member.getLocktime(); //? Date nowDate = Calendar.getInstance().getTime(); // int loginFailureLockTime = WebConstance.LOCKTIMES; if (3 == member.getUserStatus() && isUnLock(lockedDate, nowDate, loginFailureLockTime)) { //? member.setUserStatus(1); member.setLoginFailtimes(0); try { memberService.updateMember(member); } catch (ServiceException e) { log.error("?e={}", e); } catch (DataException e) { log.error("?e={}", e); } } log.info("?" + username + "?"); ShiroPrincipal subject = new ShiroPrincipal(member); //List<String> authorities = Member.dao.getAuthoritiesName(user.getStr("id")); //List<String> rolelist = Member.dao.getRolesName(user.getStr("id")); //subject.setAuthorities(authorities); //subject.setRoles(rolelist); subject.setAuthorized(true); return new SimpleAuthenticationInfo(subject, member.getPassword(), getName()); } /** * @category ??? * @param lockedDate * @param nowDate * @param loginFailureLockTime * @return */ private boolean isUnLock(Date lockedDate, Date nowDate, int loginFailureLockTime) { //?+???? Calendar calendarLock = Calendar.getInstance(); calendarLock.setTime(lockedDate); calendarLock.add(Calendar.DAY_OF_YEAR, loginFailureLockTime); Calendar calendarNow = Calendar.getInstance(); calendarNow.setTime(nowDate); if (calendarLock.getTimeInMillis() > calendarNow.getTimeInMillis()) { return false; } else { //?? return true; } } }