com.huake.service.account.ShiroDbRealm.java Source code

Java tutorial

Introduction

Here is the source code for com.huake.service.account.ShiroDbRealm.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.huake.service.account;

import java.io.Serializable;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
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.UsernamePasswordToken;
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.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.Encodes;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.huake.entity.Member;
import com.huake.entity.User;
import com.huake.service.member.CustomCredentialsMatcher;
import com.huake.service.member.MemberService;

public class ShiroDbRealm extends AuthorizingRealm {

    protected MemberService memberService;

    /**
     * ?,.
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
            throws AuthenticationException {
        /*UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        User user = accountService.findUserByLoginName(token.getUsername());
        if (user != null) {
           byte[] salt = Encodes.decodeHex(user.getSalt());
           return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getLoginName(), user.getName()),
           user.getPassword(), ByteSource.Util.bytes(salt), getName());
        } else {
           return null;
        }*/
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        Member member = memberService.findByEmail(token.getUsername(), Member.STATUS_VALID);
        if (member != null) {
            //byte[] salt = Encodes.decodeHex(member.getSalt());
            System.out.println("??  Id" + member.getMemberId() + "??" + member.getLoginName()
                    + "  " + member.getEmail() + "  ?" + member.getPassword());
            //return new SimpleAuthenticationInfo(new ShiroUser(member.getMemberId(), member.getNickName(), member.getEmail()),
            //   member.getPassword(), ByteSource.Util.bytes(salt), getName());
            return new SimpleAuthenticationInfo(
                    new ShiroUser(member.getMemberId(), member.getNickName(), member.getEmail()),
                    member.getPassword(), getName());
        } else {
            return null;
        }
    }

    /**
     * ?, ???.
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        /*ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
        User user = accountService.findUserByLoginName(shiroUser.loginName);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(user.getRoleList());
        return info;*/
        ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
        Member member = memberService.findByEmail(shiroUser.email, Member.STATUS_VALID);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(ImmutableList.copyOf(StringUtils.split(member.getRoles(), ",")));
        return info;
    }

    /**
     * PasswordHash.
     */
    @PostConstruct
    public void initCredentialsMatcher() {
        /*HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(AccountService.HASH_ALGORITHM);
        matcher.setHashIterations(AccountService.HASH_INTERATIONS);*/
        setCredentialsMatcher(new CustomCredentialsMatcher());
    }

    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }

    /**
     * AuthenticationSubject??????.
     */
    public static class ShiroUser implements Serializable {
        private static final long serialVersionUID = -1373760761780840081L;
        public Long memberId;
        public String nickName;
        public String email;

        public ShiroUser(Long memberId, String nickName, String email) {
            System.out.println("?    ID:" + memberId + "    name:" + nickName + "    email:" + email);
            this.memberId = memberId;
            this.nickName = nickName;
            this.email = email;
        }

        public String getName() {
            return nickName;
        }

        /**
         * <shiro:principal/>.
         */
        @Override
        public String toString() {
            return email;
        }

        /**
         * ?hashCode,?loginName;
         */
        @Override
        public int hashCode() {
            return Objects.hashCode(email);
        }

        /**
         * ?equals,?email;
         */
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            ShiroUser other = (ShiroUser) obj;
            if (email == null) {
                if (other.email != null) {
                    return false;
                }
            } else if (!email.equals(other.email)) {
                return false;
            }
            return true;
        }
    }
}