com.galaxy.service.user.ShiroDbRealm.java Source code

Java tutorial

Introduction

Here is the source code for com.galaxy.service.user.ShiroDbRealm.java

Source

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

import java.util.Date;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.time.DateUtils;
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.springframework.stereotype.Service;

import com.galaxy.commons.utils.Encodes;
import com.galaxy.dal.domain.user.User;

@Service
public class ShiroDbRealm extends AuthorizingRealm {
    @Autowired
    protected UserService userService;
    @Autowired
    TokenService tokenService;

    /**
     * ?,.
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
            throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        User user = userService.findUserByLoginName(token.getUsername());
        if (user != null) {
            byte[] salt = Encodes.decodeHex(user.getSalt());
            LoginUserModel loginUser = new LoginUserModel();
            loginUser.setEmail(user.getEmail());
            loginUser.setLoginName(token.getUsername());
            loginUser.setMobile(user.getMobile());
            loginUser.setNickName(user.getNick());
            loginUser.setUserId(user.getId());
            loginUser.setAvatar(user.getAvatar());
            loginUser.setToken(tokenService.generateToken());
            loginUser.setExpiredToken(tokenService.generateToken());
            loginUser.setExpireshIn(DateUtils.addDays(new Date(), 30).getTime());
            return new SimpleAuthenticationInfo(loginUser, user.getPassword(), ByteSource.Util.bytes(salt),
                    getName());
        } else {
            return null;
        }
    }

    /**
     * ?, ???.
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        LoginUserModel shiroUser = (LoginUserModel) principals.getPrimaryPrincipal();
        User user = userService.findUserByLoginName(shiroUser.loginName);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        // info.addRoles(user.getRoleList());
        return info;
    }

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

        setCredentialsMatcher(matcher);
    }

}