org.lazulite.boot.autoconfigure.osaam.shiro.realm.UserRealm.java Source code

Java tutorial

Introduction

Here is the source code for org.lazulite.boot.autoconfigure.osaam.shiro.realm.UserRealm.java

Source

/*
 * Copyright 2016. junfu
 *
 * 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.
 */

/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.lazulite.boot.autoconfigure.osaam.shiro.realm;

import org.lazulite.boot.autoconfigure.osaam.shiro.sys.user.entity.User;
import org.apache.shiro.authc.*;
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.lazulite.boot.autoconfigure.osaam.shiro.sys.auth.service.UserAuthService;
import org.lazulite.boot.autoconfigure.osaam.shiro.sys.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {

    private static final Logger log = LoggerFactory.getLogger("es-error");
    private static final String OR_OPERATOR = " or ";
    private static final String AND_OPERATOR = " and ";

    //    @Autowired
    //    public UserRealm(ApplicationContext ctx) {
    //        super();
    //        //? ?bean??????bean
    //        //why
    //        //springfindAutowireCandidatesFactoryBean????Bean?autowire
    //        //bean?bean???ObjectTypeRepository
    //        //getBean 
    //        ctx.getBeansOfType(SimpleBaseRepositoryFactoryBean.class);
    //    }
    private static final String NOT_OPERATOR = "not ";
    @Autowired
    private UserService userService;
    @Autowired
    private UserAuthService userAuthService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = (String) principals.getPrimaryPrincipal();
        User user = userService.findByUsername(username);

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setRoles(userAuthService.findStringRoles(user));
        authorizationInfo.setStringPermissions(userAuthService.findStringPermissions(user));

        return authorizationInfo;
    }

    /**
     * ?or and not ?  ??and or
     *
     * @param principals
     * @param permission
     * @return
     */
    public boolean isPermitted(PrincipalCollection principals, String permission) {
        if (permission.contains(OR_OPERATOR)) {
            String[] permissions = permission.split(OR_OPERATOR);
            for (String orPermission : permissions) {
                if (isPermittedWithNotOperator(principals, orPermission)) {
                    return true;
                }
            }
            return false;
        } else if (permission.contains(AND_OPERATOR)) {
            String[] permissions = permission.split(AND_OPERATOR);
            for (String orPermission : permissions) {
                if (!isPermittedWithNotOperator(principals, orPermission)) {
                    return false;
                }
            }
            return true;
        } else {
            return isPermittedWithNotOperator(principals, permission);
        }
    }

    private boolean isPermittedWithNotOperator(PrincipalCollection principals, String permission) {
        if (permission.startsWith(NOT_OPERATOR)) {
            return !super.isPermitted(principals, permission.substring(NOT_OPERATOR.length()));
        } else {
            return super.isPermitted(principals, permission);
        }
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String username = upToken.getUsername().trim();
        //        String password = "";
        //        if (upToken.getPassword() != null) {
        //            password = new String(upToken.getPassword());
        //        }
        User user = userService.findByUsername(username);

        if (user == null) {
            throw new UnknownAccountException();//??
        }

        //AuthenticatingRealmCredentialsMatcher????
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user.getUsername(), //??
                user.getPassword(), //?
                ByteSource.Util.bytes(user.getUsername() + user.getSalt()), //salt
                getName() //realm name
        );
        return authenticationInfo;
    }

}