shiro demo就是不用 奇怪了 springboot2.0 老师的版本现在已经没有了
来源:3-10 基于Apache Shiro权限管理Case实操-2

uareRight
2018-11-05
项目 也不提示报错 配置sql那mysql jdbc是红线 tomcat居然也能启动! 浏览器页面/index 直接也登入 根本不起作用.求老师帮我看看`在这package com.myShiro.demo2;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;
/**
-
项目启动时ShiroFilterFactoryBean 首先被初始化 然后传入securityManager 进行实列的构造
*/
@Configuration//项目启动时spring会自动配置
public class ShiroConfiguration {
@Bean(“shiroFilter”)
public ShiroFilterFactoryBean shiroFilter(@Qualifier(“securityManager”) SecurityManager securityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
bean.setSecurityManager(securityManager);
bean.setLoginUrl("/login");
bean.setSuccessUrl("/index");
bean.setUnauthorizedUrl("/unauthorized");
LinkedHashMap<String ,String> filterChainDefinitionMap = new LinkedHashMap<>();//key 使用什么样的请求, value 使用什么样的拦截器
filterChainDefinitionMap.put(“index”,“authc”);//authc 属于枚举类DefaultFilter类
filterChainDefinitionMap.put(“login”,“anon”);
bean.setFilterChainDefinitionMap(filterChainDefinitionMap);//把这些设置给setFilterChainDefinitionMap
return bean;
}/**
*- 传入 authRealm 进行构造
*/
@Bean(“securityManager”)
public SecurityManager securityManager(@Qualifier(“autRealm”) AuthRealm authRealm){
DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
manager.setRealm(authRealm);
return manager;}
/**
*- 传入 credentiaLMatcher 进行构造
*/
@Bean(“autRealm”)
public AuthRealm autRealm(@Qualifier(“credentiaLMatcher”) CredentiaLMatcher credentiaLMatcher){
AuthRealm autRealm = new AuthRealm();
autRealm.setCredentialsMatcher(credentiaLMatcher);
return autRealm;
}/**
*- 传入 CredentiaLMatcher 为自己定义的实现类 只要注入到AuthRealm这个类其包含对CredentiaLMatcher的构造方法
*/
@Bean(“credentiaLMatcher”)
public CredentiaLMatcher credentiaLMatcher(){
return new CredentiaLMatcher();
}
//--------------------------------------------------------------------------------------------------//与spring关联的配置
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier(“securityManager”) SecurityManager securityManager){
AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
advisor.setSecurityManager(securityManager);
return advisor;
}
@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator (){
DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
creator.setProxyTargetClass(true);
return creator;
}
}
package com.myShiro.demo2;
import com.myShiro.demo2.mapper.service.UserService;
import com.myShiro.demo2.model.Permission;
import com.myShiro.demo2.model.Role;
import com.myShiro.demo2.model.User;
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.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class AuthRealm extends AuthorizingRealm {
@Autowired
private UserService userService;
@Override
//授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
User user = (User) principalCollection.fromRealm(this.getClass().getName()).iterator().next();
List permissionList = new ArrayList<>();
Set roles = user.getRoles();
if(!CollectionUtils.isEmpty(roles)){
for (Role role : roles) {
Set permissions = role.getPermissions();
if(!CollectionUtils.isEmpty(permissions)){
for (Permission permission:permissions
) {
permissionList.add(permission.getName());
}
}
}
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addStringPermissions(permissionList);
return info;
}
//认证登入
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken)authenticationToken;
String username = usernamePasswordToken.getUsername();
User user = userService.findByUsername(username);
return new SimpleAuthenticationInfo(user,user.getPassword(),this.getClass().getName());
}
}
里输入代码
1回答
-
你好,首先配置页面红色部分不代表有错(如果真的有错,系统会因为这个错而无法启动);
其次,我细看了你的贴了这些代码,好像没看到实际执行的index接口代码,我需要了解你这里index实际是如何写的,才能确认你访问这个接口是否有问题。你补充一下我再看看?0212018-11-06
相似问题