com.ks.shiro.auth.controler.EntityRealm.java Source code

Java tutorial

Introduction

Here is the source code for com.ks.shiro.auth.controler.EntityRealm.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ks.shiro.auth.controler;

import com.ks.shiro.auth.entities.UserRoles;
import com.ks.shiro.auth.entities.Users;
import com.ks.shiro.auth.service.UserService;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 *
 * @author Kodjo Sama
 */
public class EntityRealm extends AuthorizingRealm {

    private UserService service;

    public EntityRealm() throws NamingException {
        setName("entityRealm");
        InitialContext context = new InitialContext();
        this.service = (UserService) context.lookup("java:global/shiro-auth/UserService");
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
        if (pc == null) {
            throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
        }
        System.out.println("auto");
        String userLogin = (String) pc.fromRealm(this.getName()).iterator().next();
        //String userLogin = (String) pc.getPrimaryPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = null;
        Users user = service.find(userLogin);
        if (user != null) {
            simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            List<UserRoles> roles = service.roles(user);
            System.out.println("roles:" + roles.get(0).getRole());
            if (roles != null) {
                for (UserRoles userRoles : roles) {
                    simpleAuthorizationInfo.addRole(userRoles.getRole());
                }
            }
        }
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken at) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) at;
        System.out.println("Data:" + token.getUsername() + "@" + String.valueOf(token.getPassword()));

        SimpleAuthenticationInfo simpleAuthenticationInfo = null;
        Users user = service.find(token.getUsername(), String.valueOf(token.getPassword()));
        System.out.println("u:" + user);
        if (user != null) {
            simpleAuthenticationInfo = new SimpleAuthenticationInfo(user.getLogin(), user.getPassword(), getName());
        }
        return simpleAuthenticationInfo;
    }

    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }
}