Shiro的授权有两种授权,一种是在配置文件中配置,一种是利用注解来完成授权
第一种,我们想要实现授权,首先得让我们的MyRealm这个继承AuthorizationRealm这个它里面有一个授权的方法
@Override
/*这个方法是授权的方法*/
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
}
首先,我们要在Spring的applicationContext.xml配置角色
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置securityManager的bean-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--配置Shiro缓存 -->
<property name="cacheManager" ref="cacheManager"/>
<!--调用配置策略的时候要放在realms前面-->
<property name="authenticator" ref="authenticator"/>
<!--配置realm(这个非常的重要)-->
<property name="realm" ref="jdbcRealm"/>
</bean>
<!--配置缓存-->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean>
<!--配置Realm-->
<bean id="jdbcRealm" class="org.peter.realm.MyRealm">
</bean>
<!--配置Spring去管理Shiro中的bean的生命周期-->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!--开启Shiro的注解使用-->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<!--配置页面过滤规则-->
<!--web.xml中filter-name的名字要和ShiroFilterFactoryBean的id一样,不一样会出错-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!--配置登录页面-->
<property name="loginUrl" value="/Login.jsp"/>
<!--配置登录成功后的页面-->
<property name="successUrl" value="/index.jsp"/>
<!--配置没有获得访问权限的页面-->
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<property name="filterChainDefinitions">
<value>
<!--anon 表示Login.jsp可以匿名访问-->
/Login.jsp = anon
/login=anon
<!--配置退出-->
/logout=logout
<!--配置角色-->
/user.jsp=roles[user]
/admin.jsp=roles[admin]
<!--authc表示剩余的页面必须在登录后访问-->
/** = authc
</value>
</property>
</bean>
</beans>
第二步:配置MyRealm的类
package org.peter.realm;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import java.util.HashSet;
import java.util.Set;
/**
* Created by Lenovo on 2017/8/1.
*/
public class MyRealm extends AuthorizingRealm {
@Override
public String getName() {
return "MyRealm";
}
@Override
public boolean supports(AuthenticationToken token) {
return token instanceof UsernamePasswordToken;
}
// MD5消息摘要(密码加密)
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token1) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) token1;
/*获取到用户名*/
String username = token.getUsername();
if ("zs".equals(username)){
throw new UnknownAccountException("用户名不存在");
}
if ("ls".equals(username)){
throw new LockedAccountException("该用户已经被锁");
}
// 取数据库中在查询
// ....................
Object credentials = null;
if ("admin".equals(username)){
credentials="c41d7c66e1b8404545aa3a0ece2006ac";
}else if("wangwu".equals(username)){
credentials="b6f8daff78cfcb486454e6914e666c71";
}
ByteSource credentialsSalt = ByteSource.Util.bytes(username);
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, credentials,credentialsSalt, getName());
return info;
}
public static void main(String[] args) {
// 解密:
ByteSource salt = ByteSource.Util.bytes("wangwu");
SimpleHash simpleHash = new SimpleHash("MD5","123",salt,1024);
System.out.println(simpleHash);
}
@Override
/*这个方法是授权的方法*/
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
Object principal = principalCollection.getPrimaryPrincipal();
Set<String> roles = new HashSet<>();
roles.add("user");
if ("admin".equals(principal)){
roles.add("admin");
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
return info;
}
}
第三步:再利用Shiro的标签来实现相应的角色可以看到相应的jsp
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎页面</title>
</head>
<body>
<h1>热烈欢迎</h1>
<a href="/test">test</a>
welcome<shiro:principal/><br>
<a href="/logout">退出</a><br>
<shiro:hasRole name="admin">
<a href="/admin.jsp">admin.jsp</a><br>
</shiro:hasRole>
<shiro:hasRole name="user">
<a href="/user.jsp">user.jsp</a><br>
</shiro:hasRole>
</body>
</html>
Shiro的第二种注解的方式实现授权
为了看到Shiro注解实现授权
第一步:创建一个MyService类,里面定义一个Test方法
package org.peter.service;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Service;
/**
* Created by Lenovo on 2017/8/1.
*/
@Service
public class MyService {
@RequiresRoles({"admin"})
public void test(){
System.out.println("hahhhahahahah");
}
}
第二步:在Controller里面配置
package org.peter.controller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.peter.service.MyService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
/**
* Created by Lenovo on 2017/8/1.
*/
@Controller
public class HelloController {
/*测试*/
@Resource
MyService myService;
@RequestMapping("/test")
public void test(){
myService.test();
}
}
第三步:在jsp的页面里添加一个超链接来测试
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎页面</title>
</head>
<body>
<h1>热烈欢迎</h1>
<a href="/test">test</a>
</body>
</html>