com.beginner.core.shiro.BeginnerRealm.java Source code

Java tutorial

Introduction

Here is the source code for com.beginner.core.shiro.BeginnerRealm.java

Source

/*
 * Copyright 2015-9999 the original author or authors.
 *
 * 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.beginner.core.shiro;

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.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * <b>??</b>BeginnerRealm<br/>
 * <b>??</b><br/>
 * <b></b>Hsiao Lin Studio<br/>
* <b></b><br/>
* <b></b>20150521 ?6:18:18<br/>
* <b></b><br/>
* @version 1.0.0<br/>
*/
public class BeginnerRealm extends AuthorizingRealm {

    // @Resource
    // private UserService userService;

    /*
     * ????(non-Javadoc)
     * 
     * @see
     * org.apache.shiro.realm.AuthenticatingRealm#doGetAuthenticationInfo(org
     * .apache.shiro.authc.AuthenticationToken)
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String username = (String) token.getPrincipal(); // ??
        String password = new String((char[]) token.getCredentials()); // ?

        if (null != username && null != password) {
            return new SimpleAuthenticationInfo(username, password, getName());
        } else {
            return null;
        }

    }

    /*
     * ?, ???,?(non-Javadoc)
     * 
     * @see
     * org.apache.shiro.realm.AuthorizingRealm#doGetAuthorizationInfo(org.apache
     * .shiro.subject.PrincipalCollection)
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
        try {
            /*
             * Collection<?> collection = pc.fromRealm(getName()); if
             * (CollectionUtils.isEmpty(collection)) { return null; } // 
             * String username = (String) collection.iterator().next(); //
             * ??? User user =
             * userService.getUserRolePermissionByName(username); // 
             * Set<Role> roles = user.getRoleSet(); List<String> rLst = new
             * ArrayList<String>(); List<String> pLst = new ArrayList<String>();
             * if (!CollectionUtils.isEmpty(roles)) { for (Role role : roles)
             * {//  int valid = role.getIsValid(); if (0 == valid) {// 
             * if (!StringUtils.isEmpty(role.getRoleCode()))
             * rLst.add(role.getRoleCode()); Set<Permissions> permissionSet =
             * role.getPermissionSet(); if (!CollectionUtils.isEmpty(roles)) {
             * for (Permissions permissions : permissionSet) {// ?? String
             * isValid = permissions.getIsValid(); if (null == isValid ||
             * "0".equals(isValid)) {// ?? if
             * (!StringUtils.isEmpty(permissions.getPermCode()))
             * pLst.add(permissions.getPermCode()); } } } } } }
             * 
             * SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
             * info.addRoles(rLst); info.addStringPermissions(pLst); return
             * info;
             */
            return new SimpleAuthorizationInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}