com.masslink.idea.zigbee.shiro.UserRealm.java Source code

Java tutorial

Introduction

Here is the source code for com.masslink.idea.zigbee.shiro.UserRealm.java

Source

/*
 * Copyright 2017 Masslink technology Ltd., Co.
 * Licensed under the Apache License 2.0.
 */
package com.masslink.idea.zigbee.shiro;

import com.masslink.idea.zigbee.domain.User;
import com.masslink.idea.zigbee.service.UserService;
import javax.annotation.Resource;
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;

/**
 * ZigBeeRealm
 *
 * @author L.X <gugia@qq.com>
 */
public class UserRealm extends AuthorizingRealm {

    Logger logger = LoggerFactory.getLogger(UserRealm.class);

    @Resource
    UserService userService;

    /**
     * Shiro ???
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = principals.getPrimaryPrincipal().toString();
        User user = userService.findByName(username);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        String role = userService.getRole(user.getAuth());
        authorizationInfo.addRole(role);
        return authorizationInfo;
    }

    /**
     * Shiro ?
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        User user = userService.findByName(username);
        if (user == null) {
            throw new UnknownAccountException();//?
        }
        if (user.getAuth() == 0) {
            throw new DisabledAccountException();//?
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),
                this.getName());
        return info;
    }
}