com.migo.shiro.UserRealm.java Source code

Java tutorial

Introduction

Here is the source code for com.migo.shiro.UserRealm.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 com.migo.shiro;

import com.migo.entity.SysMenuEntity;
import com.migo.entity.SysUserEntity;
import com.migo.service.SysMenuService;
import com.migo.service.SysUserService;
import org.apache.commons.lang.StringUtils;
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.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 
 * @email fei6751803@163.com
 */
public class UserRealm extends AuthorizingRealm {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * ?(???)
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SysUserEntity user = (SysUserEntity) principalCollection.getPrimaryPrincipal();
        Long userId = user.getUserId();

        List<String> permsList;

        //???
        if (userId == 1) {
            List<SysMenuEntity> menuList = sysMenuService.queryList(new HashMap<>());
            permsList = menuList.stream().parallel().map(SysMenuEntity::getPerms).collect(Collectors.toList());
            /*permsList = new ArrayList<>(menuList.size());
            for(SysMenuEntity menu : menuList){
            permsList.add(menu.getPerms());
            }*/
        } else {
            permsList = sysUserService.queryAllPerms(userId);
        }

        //??
        /* Set<String> permsSet = new HashSet<>();
         for(String perms : permsList){
        if(StringUtils.isBlank(perms)){
            continue;
        }
        permsSet.addAll(Arrays.asList(perms.trim().split(",")));
         }*/
        Set<String> permsSet = permsList.stream().parallel().filter(StringUtils::isNotBlank).map(String::trim)
                .map(s -> s.split(",")).map(Arrays::asList).flatMap(Collection::stream).collect(Collectors.toSet());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setStringPermissions(permsSet);
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
            throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        String password = new String((char[]) authenticationToken.getCredentials());

        //?
        SysUserEntity user = sysUserService.queryByUserName(username);

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

        //?
        if (!password.equals(user.getPassword())) {
            throw new IncorrectCredentialsException("???");
        }

        //??
        if (user.getStatus() == 0) {
            throw new LockedAccountException("??,??");
        }

        return new SimpleAuthenticationInfo(user, password, getName());
    }

}