shiro集成spring

创建项目

shiro集成spring_第1张图片

pom引包

 
        
        
            org.springframework
            spring-context
            4.3.5.RELEASE
        

        
        
            org.springframework
            spring-webmvc
            4.3.5.RELEASE
        

        
        
            org.apache.shiro
            shiro-core
            1.4.0-RC2
        
        
            org.apache.shiro
            shiro-spring
            1.4.0-RC2
        
        
        
            org.apache.shiro
            shiro-web
            1.4.0-RC2
        

    


web.xml




    
        shiroFilter
        org.springframework.web.filter.DelegatingFilterProxy
    
    
        shiroFilter
        /*
    

    
        contextConfigLocation
        classpath:spring/spring.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring/springmvc.xml
        
        1
        true
    

    
        DispatcherServlet
        /
    

    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        CharacterEncodingFilter
        /*
    

spring.xml




    
    
        
        
        
        
            
                /login.html = anon
                /subLogin = anon
                /* = authc
            
        
    

    
    
    
        
    

    
    
        
    

    
    
        
        
    

springmvc.xml



    
    

    

    

UserController

package xdong.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import xdong.vo.User;

/**
*  用户登录请求
*  @author : xdong
*  @create : 15:56 2018/6/28
*  @Param :
*  @return :
**/
@RestController
public class UserController {

    @PostMapping(value = "/subLogin",produces = "application/json;charset=utf-8")
    public String subLogin(User user){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());

        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            return e.getMessage();
        }

        return "登陆成功";
    }
}

CustomRealm

package xdong.shiro.realm;

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;
import org.apache.shiro.util.ByteSource;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
*  自定义Realm
*  @author : xdong
*  @create : 14:29 2018/6/28
*  @Param :
*  @return :
**/
public class CustomRealm extends AuthorizingRealm{

    Map userMap = new HashMap<>(16);
    {
        userMap.put("Mark","d40fdd323f5322ff34a41f026f35cf20");
        super.setName("cuseomRealm");
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
       //通过用户获取角色数据
        String userName = (String) principalCollection.getPrimaryPrincipal();
        //从数据库或缓存中获取数据
        Set roles = getRolesByUserName(userName);
        Set permissions = getPermissionsByUserName(userName);

        SimpleAuthorizationInfo simpleAuthorizationInfo =new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setRoles(roles);
        simpleAuthorizationInfo.setStringPermissions(permissions);
        return simpleAuthorizationInfo;
    }

    private Set getPermissionsByUserName(String userName) {
        Set sets =new HashSet<>();
        sets.add("user:delete");
        sets.add("user:add");
        return sets;
    }

    /**
     * 模拟从数据库或缓存中获取数据
     * @param userName
     * @return
     */
    private Set getRolesByUserName(String userName) {
        Set sets =new HashSet<>();
        sets.add("admin");
        sets.add("user");
        return  sets;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1. 从主题传过来的认证信息中,获取用户名
        String userName = (String) authenticationToken.getPrincipal();

        //2. 用过用户名取数据中获取凭证
        String password= getPasswordByUserName(userName);
        if(password == null){
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo =new SimpleAuthenticationInfo
                ("Mark",password,"cuseomRealm");
        authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("Mark"));
        return authenticationInfo;
    }


    /**
     * 模拟数据库查询凭证
     * @param userName
     * @return
     */
    private String getPasswordByUserName(String userName) {
        return userMap.get(userName);
    }


}

User

package xdong.vo;

public class User {
    private String username;

    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

shiro集成spring_第2张图片

Mark
1234567
shiro集成spring_第3张图片
Mark
123999


你可能感兴趣的:(shiro集成spring)