1.在web.xml文件中配置shiro的过滤器,以拦截项目内的访问
<!-- shiro过滤器定义 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.在spring 的配置文件中配置shiro的相关项
<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 缓存管理 -->
<bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>
<!-- 自定义Realm -->
<bean id="myRealm" class="com.bf.planner.realm.MyRealm"/> <!-- 自定义reaml获取令牌以及角色和权限的类 -->
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm"/>
</bean>
<!-- Shiro过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- Shiro的核心安全接口,这个属性是必须的 -->
<property name="securityManager" ref="securityManager"/>
<!-- 身份认证失败,则跳转到登录页面的配置 -->
<property name="loginUrl" value="/"/>
<!-- 权限认证失败,则跳转到指定页面 -->
<property name="unauthorizedUrl" value="/unauthor.jsp"/>
<!-- Shiro连接约束配置,即过滤链的定义 -->
<property name="filterChainDefinitions">
<value>
/** = authc <!-- 配置访问项目下的所有路径均需要验证令牌(即登录) -->
</value>
</property>
</bean>
public class MyRealm extends AuthorizingRealm{
@Autowired
private LoginService loginService;
/**
* 用户授权
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String userName=(String)principals.getPrimaryPrincipal(); //获取当前登录用户的用户名
SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo(); //新建权限的对象
authorizationInfo.setRoles(loginService.getRoles(userName)); //设置此用户对应的角色
authorizationInfo.setStringPermissions(loginService.getPermissions(userName)); //设置此用户对应的权限
return authorizationInfo;
}
/**
* 用户登录
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String user_name=(String)token.getPrincipal();//获取登录者的用户名
User user=loginService.getUser(user_name); //根据用户名查出数据库中所对应的用户信息
if(user!=null){
AuthenticationInfo authcInfo=new SimpleAuthenticationInfo(user.getUser_name(),user.getPassword(),"xx");
//设置此用户的令牌,即设置正确的用户名与密码
return authcInfo;
}else{
return null;
}
}
}
@RequestMapping("/login")
@ResponseBody
public ResultInfo login(User user){
ResultInfo resultInfo=new ResultInfo();
Subject subject=SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken(user.getUser_name(), user.getPassword());
try{
subject.login(token);
resultInfo.setCode(true); //登录成功
return resultInfo;
}catch(Exception e){
resultInfo.setCode(false); //登录失败,如果没有对应的用户,则会抛出异常
return resultInfo;
}
}
5.使用shiro来控制方法的访问权限(例子):
@RequiresRoles(value={"admin","base"}, logical = Logical.OR)
@RequiresPermissions(value = {"add","update"}, logical = Logical.AND)
public void shiro_test(){
}
上面的代码中对shiro_test方法添加了访问限制:
@RequiresRoles控制拥有admin或base角色的用户可以访问此方法
@RequiresPermissions控制拥有add和update权限的用户可以访问此方法
6.页面中使用shiro来控制页面内容的显示:
首先,需要在页面头部引入shiro支持:
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
然后,我们可以在页面中使用shiro的标签了:(<shiro:principal/>当前登录用户的用户名【即登录账号】)<shiro:hasRole name="admin">
欢迎有admin角色的用户!<shiro:principal/> <!-- 登录人 -->
</shiro:hasRole>
<shiro:hasPermission name="student:create">
欢迎有student:create权限的用户!<shiro:principal/>
</shiro:hasPermission>
注意:从数据库中获取的角色集合和权限集合中不允许有空值