cn.ligoo.part.service.shiro.CustomAuthorizingRealm.java Source code

Java tutorial

Introduction

Here is the source code for cn.ligoo.part.service.shiro.CustomAuthorizingRealm.java

Source

/*
 * 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 cn.ligoo.part.service.shiro;

import java.io.Serializable;

import org.apache.shiro.SecurityUtils;
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.DisabledAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
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.util.Assert;

import cn.ligoo.part.entity.UserInfo;
import cn.ligoo.part.service.UserInfoService;
import cn.ligoo.part.web.Constants;

import com.google.common.base.Objects;

public class CustomAuthorizingRealm extends AuthorizingRealm {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private UserInfoService userInfoService;

    /**
     * ?,.
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
            throws AuthenticationException {
        logger.debug("...CustomAuthorizingRealm.doGetAuthenticationInfo()");
        CustomToken token = (CustomToken) authcToken;

        String username = token.getUsername();
        if (username == null) {
            throw new AccountException();
        }

        UserInfo user = userInfoService.findByEmail(username);
        if (user == null) {
            throw new UnknownAccountException();
        }

        if (user.getIs_del() == Constants.BYTE_1) {
            throw new DisabledAccountException();
        }
        return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getEmail()), user.getPassword(),
                getName());

    }

    /**
     * ?, ???.
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        logger.debug("...CustomAuthorizingRealm.doGetAuthorizationInfo()");
        ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
        Assert.notNull(shiroUser, "?principalsshiroUser");

        UserInfo user = userInfoService.findByEmail(shiroUser.email);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        logger.debug("...add role admin");
        info.addRole("admin");

        SecurityUtils.getSubject().getSession().setAttribute(Constants.SESSION_USER_INFO, user);
        return info;
    }

    public void setUserInfoService(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

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

        public ShiroUser(Long id, String email) {
            this.id = id;
            this.email = email;
        }

        public String getEmail() {
            return email;
        }

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

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

        /**
         * ?equals,?loginName;
         */
        @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;
        }
    }
}