spring mvc整合shiro无法访问控制器是啥问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring mvc整合shiro无法访问控制器是啥问题相关的知识,希望对你有一定的参考价值。
参考技术A 1.概述现在的项目使用的权限控制系统是spring security 3.因为项目的框架使用spring,就顺便使用了。最近研究了一下spring
side4,推荐使用shiro。照着示例做了一遍。在原有的spring web工程中。步骤如下。
2.引进包,maven设置
[html] view plaincopy
org.apache.shiro
shiro-all
1.2.1
jar
compile
3.实现Controller层
主要是登陆url和几个掩饰url
[java] view plaincopy
@Controller
public class AdminController
@RequestMapping(value = "/admin/index", method = RequestMethod.GET)
public String index(Model model)
return "admin/index";
@RequestMapping(value = "/admin/login", method = RequestMethod.GET)
public String login(Model model)
logger.info("login get");
return "admin/login";
@RequestMapping(value = "/admin/login", method = RequestMethod.POST)
public String doLogin(Model model)
logger.info("login post");
return "admin/login";
@RequiresRoles("user")
@RequestMapping(value = "/admin/user", method = RequestMethod.GET)
public String shiroUser(Model model)
return "admin/index";
@RequiresRoles("admin")
@RequestMapping(value = "/admin/admin", method = RequestMethod.GET)
public String shiroAdmin(Model model)
return "admin/index";
Logger logger = LoggerFactory.getLogger(AdminController.class);
4.实现权限验证
继承shiro的AuthorizingRealm
[java] view plaincopy
public class ShiroDbRealm extends AuthorizingRealm
protected AccountService accountService;
@Autowired
public void setAccountService(AccountService accountService)
this.accountService = accountService;
/**
* 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
*/
@SuppressWarnings("unused")
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection p)
logger.info("授权认证:" + p.getRealmNames());
ShiroUser shiroUser = (ShiroUser) p.getPrimaryPrincipal();
&nbsnbsp;User user =
accountService.findUserByLoginName(shiroUser.loginName);
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
for (Role role : user.getRoleList())
//基于Role的权限信息
info.addRole(role.getName());
//基于Permission的权限信息
info.addStringPermission(role.getPermissions());
return info;
/**
* 认证回调函数,登录时调用.
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken) throws AuthenticationException
logger.info("authc pass:");
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
logger.info("authc name:" + token.getUsername());
User user = accountService.findUserByLoginName(token.getUsername());
if (user != null)
if (user.getStatus().equals("disabled"))
throw new DisabledAccountException();
logger.info("authc name:" + token.getUsername() + " user:"
+ user.getLoginName() + " pwd:" + user.getPassword()
+ "getname:" + getName());
// byte[] salt = Encodes.decodeHex(user.getSalt());
return new SimpleAuthenticationInfo(new ShiroUser(user.getLoginName(),
user.getName()),
user.getPassword(), getName());
return null;
/**
* 自定义Authentication对象,使得Subject除了携带用户的登录名外还可以携带更多信息.
*/
public static class ShiroUser implements Serializable
private static final long serialVersionUID = -1373760761780840081L;
public String loginName;
public String name;
public ShiroUser(String loginName, String name)
this.loginName = loginName;
this.name = name;
public String getName()
return loginName;
/**
* 本函数输出将作为默认的输出.
*/
@Override
public String toString()
return loginName;
/**
* 重载hashCode,只计算loginName;
*/
@Override
public int hashCode()
return Objects.hashCode(loginName);
/**
* 重载equals,只计算loginName;
*/
@Override
public boolean equals(Object obj)
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ShiroUser other = (ShiroUser) obj;
if (loginName == null)
if (other.loginName != null)
return false;
else if (!loginName.equals(other.loginName))
return false;
return true;
Logger logger = LoggerFactory.getLogger(ShiroDbRealm.class);
自定义的类ShiroUser是为了,可以多传输一些内容,供后面验证时使用,例子中只用了一个loginName,一般可以用String
传输就够了。
登陆时用doGetAuthenticationInfo()函数获得相关信息。
登陆后访问url 使用doGetAuthorizationInfo()获得用户的权限。
5.配置文件shiro部分
将controller的url纳入权限验证范围。
[html] view plaincopy
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"
default-lazy-init="true">
Shiro安全配置
/admin/login = authc
/admin/logout = logout
/static/** = anon
/admin/** = authc
主要内容是shiroFilter的定义。
loginUrl:登陆页面,用户登陆不成功,自动返回此页面。
successUrl:登陆成功后跳转此页面
unauthorizedUrl:用户访问无权限的链接时跳转此页面
filterChainDefinitions:设置url的访问权限。anon表示不用验证,都可以访问。anthc:authc filter
监听,不登陆不能访问。logout:logout filter监听。没有列出的常用配置:perms["remote:invoke"] :需要角色romote
和权限invoke才能访问。roles["admin"]需要角色admin才能访问。设置可用“,”隔开,如:
/admin/test = authc,roles[admin]
关于filter的列表:
Filter NameClass
anonorg.apache.shiro.web.filter.authc.AnonymousFilter
authcorg.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasicorg.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
permsorg.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
portorg.apache.shiro.web.filter.authz.PortFilter
restorg.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
rolesorg.apache.shiro.web.filter.authz.RolesAuthorizationFilter
sslorg.apache.shiro.web.filter.authz.SslFilter
userorg.apache.shiro.web.filter.authc.UserFilter
以上是关于spring mvc整合shiro无法访问控制器是啥问题的主要内容,如果未能解决你的问题,请参考以下文章
Shiro系列之Shiro+Spring MVC整合(Integration)
Shiro系列之Shiro+Spring MVC整合(Integration)
Shiro系列之Shiro+Spring MVC整合(Integration)
Shiro系列之Shiro+Spring MVC整合(Integration)