定义两个realm
adminRealm
userRealm
1定义LoginType 枚举类
public enum LoginType { USER("User"), ADMIN("Admin"); private String type; private LoginType(String type) { this.type = type; } @Override public String toString() { return this.type.toString(); } }2 定义CustomizedToken 继承 UsernmaePasswordToken
//登录类型,判断是普通用户登录,教师登录还是管理员登录 private String loginType; public CustomizedToken(final String username, final String password,String loginType) { super(username,password); this.loginType = loginType; } public String getLoginType() { return loginType; } public void setLoginType(String loginType) { this.loginType = loginType; }
3 ,添加 MyFormAuthenticationFilter 继承 FormAuthenticationFiler
添加loginType 属性
生成get set
重写 createToken 方法
@Override protected CustomizedToken createToken(ServletRequest request, ServletResponse response) { String username = getUsername(request); String password = getPassword(request); String loginType = getLoginType(request); return new CustomizedToken(username,password,loginType); } protected String getLoginType(ServletRequest request) { return WebUtils.getCleanParam(request, this.getLoginType()); }
4,定义 CustimizedModularRealmAuthenticator 继承 ModularRealmAuthenticator
重写 doAuthenticate 匹配原则 是否包含 也可以修改取下标 自己定义自己喜欢的方式
@Override protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException { // 判断getRealms()是否返回为空 assertRealmsConfigured(); // 强制转换回自定义的CustomizedToken CustomizedToken customizedToken = (CustomizedToken) authenticationToken; // 登录类型 String loginType = customizedToken.getLoginType(); // 所有Realm Collection<Realm> realms = getRealms(); // 登录类型对应的所有Realm Collection<Realm> typeRealms = new ArrayList<>(); for (Realm realm : realms) { if (realm.getName().contains(loginType)) typeRealms.add(realm); } // 判断是单Realm还是多Realm if (typeRealms.size() == 1) return doSingleRealmAuthentication(typeRealms.iterator().next(), customizedToken); else return doMultiRealmAuthentication(typeRealms, customizedToken);
}
5, 修改shiroConfig配置文件, 配置多realm realm 认证方式 ,一些代码就不粘贴了,应该都回写 不会写可以给我留言
@Bean(name = "securityManager") public SecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); //设置realm. //securityManager.setRealm(adminRealm()); //设置多realm List<Realm> realms = new ArrayList<>(); realms.add(adminRealm()); realms.add(userRealm()); securityManager.setRealms(realms); // 自定义缓存实现 使用redis securityManager.setCacheManager(cacheManager()); securityManager.setAuthenticator(customizedModularRealmAuthenticator()); // 自定义session管理 使用redis securityManager.setSessionManager(sessionManager()); //注入记住我管理器; //securityManager.setRememberMeManager(rememberMeManager()); return securityManager; }
/** * 配置使用自定义认证器,可以实现多Realm认证,并且可以指定特定Realm处理特定类型的验证 * @return */ @Bean public CustomizedModularRealmAuthenticator customizedModularRealmAuthenticator(){ CustomizedModularRealmAuthenticator customizedModularRealmAuthenticator = new CustomizedModularRealmAuthenticator(); //配置认证策略,只要有一个Realm认证成功即可,并且返回所有认证成功信息 customizedModularRealmAuthenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy()); List<Realm> realms = new ArrayList<>(); realms.add(adminRealm()); realms.add(userRealm()); customizedModularRealmAuthenticator.setRealms(realms); return customizedModularRealmAuthenticator; }
@Bean public AdminRealm adminRealm() { AdminRealm realm = new AdminRealm(); /*realm.setCachingEnabled(true); realm.setAuthenticationCachingEnabled(true); realm.setAuthenticationCacheName("authenticationCache"); realm.setAuthorizationCachingEnabled(true); realm.setAuthorizationCacheName("authorizationCache");*/ realm.setCredentialsMatcher(getRetryLimitHashedCredentialsMatcher()); realm.setCacheManager(cacheManager()); return realm; } @Bean public UserRealm userRealm() { UserRealm realm = new UserRealm(); /*realm.setCachingEnabled(true); realm.setAuthenticationCachingEnabled(true); realm.setAuthenticationCacheName("authenticationCache"); realm.setAuthorizationCachingEnabled(true); realm.setAuthorizationCacheName("authorizationCache");*/ realm.setCredentialsMatcher(getRetryLimitHashedCredentialsMatcher()); realm.setCacheManager(cacheManager()); return realm; }