springboot篇】十五. springboot整合shiro多Realm控制

springboot整合shiro多Realm控制

中国加油,武汉加油!

篇幅较长,配合右边目录观看

项目准备

  1. 创建springboot项目nz1904-springboot-08-manyroles
  2. 导入spring的web包,lombox,thymeleaf包及shiro包
    <!-- spring跟shiro整合依赖-->
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-spring</artifactId>
        <version>1.3.2</version>
    </dependency>
    

1. 案例

1.1 自定义类增强UsernamePasswordToken

package com.wpj.token;

import org.apache.shiro.authc.UsernamePasswordToken;

public class CustomToken extends UsernamePasswordToken {

    // 定义登录类型,为了在后面的时候中校验使用那个realm
    private String loginType;


    public CustomToken(String userName, String password, String loginType){
        super(userName,password);
        this.loginType = loginType;
    }

    public String getLoginType() {
        return loginType;
    }

    public void setLoginType(String loginType) {
        this.loginType = loginType;
    }

}

1.2 自定义ModularRealmAuthenticator来选择使用哪个realm来进行认证

package com.wpj.authticator;

import com.wpj.token.CustomToken;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;

/**
 * 在这里选择使用哪个realm来进行认证
 */
public class CustomModularRealmAuthenticator extends ModularRealmAuthenticator {

    /**
     * 通过传入数据类型来选择使用哪个realm
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 做realm的校验
        assertRealmsConfigured();

        CustomToken customToken = (CustomToken) authenticationToken;
        String loginType = customToken.getLoginType();

        return null;

    }
}
发布了59 篇原创文章 · 获赞 11 · 访问量 4747

猜你喜欢

转载自blog.csdn.net/TheNew_One/article/details/104543754