shiro知识点 含多realms认证以及授权实例(SpringMVC + Spring + shiro)

1.获取subject(用户信息)

Subject subject = SecurituUtils.getSubject();

2.获取Session(会话管理)

Session session = subject.getSession();

3.测试当前用户是否已经被认证,即是否登陆

subject.isAuthenticated();

4.校验登陆

UsernamePasswordToken token = new UsernamePasswordToke(“账号”,”密码”);

subject.login()

5.检测是否有该角色

subject.hasRole(“角色名字”);

6.检测角色是否有该权限

subject.isPermitted(“类型:权限”);

实例:subject.isPermitted(“user:save”);

7.检测角色是否有具体权限

subject.isPermitted(“类型:权限:对象”);

实例subject.isPermitted(“user:delete:zhangsan”);

8.登出

subject.logout();

9.Shiro 路径拦截采取第一次匹配优先的方式,即从头开始使用第一个匹配的url模式对应的拦截器链。

/** = authc

/list = anon

则list页面是无法访问的。

10.Shiro 认证过程

11.Shiro 采取MD5加密添加盐值避免密码重复

可以通过账号获取盐值

Object credentials = password;
Object principal = username;
String realName = getName(); 
ByteSource salt = ByteSource.Util.bytes(username);
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, credentials, salt, realName);

12.HttpSession 保存的键值对可以通过shiro中的Session获取

 

13.多realms认证以及授权(SpringMVC + Spring + Shiro )

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>NEWssm</groupId>
  <artifactId>NEWssm</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>NEWssm Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
      <!-- spring版本号 -->
      <spring.version>4.0.2.RELEASE</spring.version>
      <!-- mybatis版本号 -->
      <mybatis.version>3.2.6</mybatis.version>
      <!-- log4j日志文件管理包版本 -->
      <slf4j.version>1.7.7</slf4j.version>
      <log4j.version>1.2.17</log4j.version>
      <lucene.version>6.0.1</lucene.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
      <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>2.3.0</version>
      </dependency>
      <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.3.1</version>
      </dependency>
      <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.3.3</version>
      </dependency>
      <dependency>
          <groupId>javax.websocket</groupId>
          <artifactId>javax.websocket-api</artifactId>
          <version>1.1</version>
          <scope>provided</scope>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-websocket</artifactId>
          <version>4.0.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-messaging</artifactId>
          <version>4.0.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-oxm</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>${spring.version}</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>${spring.version}</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${spring.version}</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>${spring.version}</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>${spring.version}</version>
      </dependency>
      <!-- mybatis核心包 -->
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>${mybatis.version}</version>
      </dependency>
      <!-- mybatis/spring包 -->
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.2.2</version>
      </dependency>
      <!-- 导入java ee jar 包 -->
      <dependency>
          <groupId>javax</groupId>
          <artifactId>javaee-api</artifactId>
          <version>7.0</version>
          <scope>provided</scope>
      </dependency>
      <!-- 导入Mysql数据库链接jar包 -->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.30</version>
      </dependency>
      <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
      <dependency>
          <groupId>commons-dbcp</groupId>
          <artifactId>commons-dbcp</artifactId>
          <version>1.2.2</version>
      </dependency>
      <!-- JSTL标签类 -->
      <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>
      <!-- 日志文件管理包 -->
      <!-- log start -->
      <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>${log4j.version}</version>
      </dependency>


      <!-- 格式化对象,方便输出日志 -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.1.41</version>
      </dependency>

      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>${slf4j.version}</version>
      </dependency>

      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>${slf4j.version}</version>
      </dependency>
      <!-- log end -->
      <!-- 映入JSON -->
      <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
      <dependency>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-mapper-asl</artifactId>
          <version>1.9.13</version>
      </dependency>
      <!-- 上传组件包 -->
      <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
      </dependency>
      <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.4</version>
      </dependency>
      <dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.9</version>
      </dependency>
      <!--加入lucene-->
      <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
      <dependency>
          <groupId>org.apache.lucene</groupId>
          <artifactId>lucene-core</artifactId>
          <version>${lucene.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
      <dependency>
          <groupId>org.apache.lucene</groupId>
          <artifactId>lucene-queryparser</artifactId>
          <version>${lucene.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common -->
      <dependency>
          <groupId>org.apache.lucene</groupId>
          <artifactId>lucene-analyzers-common</artifactId>
          <version>${lucene.version}</version>
      </dependency>

      <!--lucene中文分词-->
      <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-smartcn -->
      <dependency>
          <groupId>org.apache.lucene</groupId>
          <artifactId>lucene-analyzers-smartcn</artifactId>
          <version>${lucene.version}</version>
      </dependency>

      <!--lucene高亮-->
      <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-highlighter -->
      <dependency>
          <groupId>org.apache.lucene</groupId>
          <artifactId>lucene-highlighter</artifactId>
          <version>${lucene.version}</version>
      </dependency>

      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
      </dependency>

      <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
      <dependency>
          <groupId>aopalliance</groupId>
          <artifactId>aopalliance</artifactId>
          <version>1.0</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/aspectj/aspectjrt -->
      <dependency>
          <groupId>aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>1.5.4</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
      <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.9.1</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
      <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-core</artifactId>
          <version>1.2.5</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-web -->
      <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-web</artifactId>
          <version>1.2.5</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->
      <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-spring</artifactId>
          <version>1.2.4</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-ehcache -->
      <dependency>
          <groupId>org.apache.shiro</groupId>
          <artifactId>shiro-ehcache</artifactId>
          <version>1.2.4</version>
      </dependency>

      <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
      <dependency>
          <groupId>com.mchange</groupId>
          <artifactId>c3p0</artifactId>
          <version>0.9.5.2</version>
      </dependency>

  </dependencies>

  <build>
    <finalName>NEWssm</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

1)applicationContext-shiro.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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
   <!--多Realm登陆认证 -->
   <bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
      <property name="realms">
         <list>
            <ref bean="userRealm"/>
            <ref bean="secondRealm"/>
         </list>
      </property>
   </bean>
   <!-- 配置多个Realm 授权授权-->
   <bean id="authorizer" class="org.apache.shiro.authz.ModularRealmAuthorizer">
      <property name="realms">
         <list>
            <ref bean="userRealm"/>
            <ref bean="secondRealm"/>
         </list>
      </property>
   </bean>

   <!-- 配置緩存管理器 -->
   <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
      <!-- 指定 ehcache 的配置文件,下面会给到 -->
      <property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />
   </bean>
   <!-- 配置进行授权和认证的 userRealm,要新增一个java类来实现,下面会有,class=包名.类名,init-methood是初始化的方法 -->
   <!-- 配置 Shiro 的 SecurityManager Bean. -->
   <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
      <property name="cacheManager" ref="cacheManager" />
      <property name="authenticator" ref="authenticator"></property>
      <property name="authorizer" ref="authorizer" ></property>
   </bean>
   <bean id="userRealm" class="com.bzj.shiro.userRealm">
      <property name="credentialsMatcher">
         <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
            <property name="hashAlgorithmName" value="MD5"/>
            <property name="hashIterations" value="1024" />
         </bean>
      </property>
   </bean>
   <bean id="secondRealm" class="com.bzj.shiro.secondRealm">
      <property name="credentialsMatcher">
         <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
            <property name="hashAlgorithmName" value="SHA1"/>
            <property name="hashIterations" value="1024" />
         </bean>
      </property>
   </bean>
   <!-- 配置 Bean 后置处理器: 会自动的调用和 Spring 整合后各个组件的生命周期方法. -->
   <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />


   <!-- 配置 ShiroFilter bean: 该 bean 的 id 必须和 web.xml 文件中配置的 shiro filter 的 
      name 一致 -->
   <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
      <!-- 装配 securityManager -->
      <property name="securityManager" ref="securityManager" />
      <!-- 配置登陆页面 -->
      <property name="loginUrl" value="index.jsp" />
      <property name="successUrl" value="success.jsp" />
      <property name="unauthorizedUrl" value="unauthorizedUrl.jsp" />
      <!--<property name="filterChainDefinitionMap" ref="filterChainDefinitionMap" />-->
      <!-- 登陆成功后的一面 -->
      <!-- <property name="successUrl" value="success.jsp"/> -->
      <!-- <property name="unauthorizedUrl" value="/shiro-unauthorized.jsp"/> -->
      <!-- 具体配置需要拦截哪些 URL, 以及访问对应的 URL 时使用 Shiro 的什么 Filter 进行拦截. -->
      <property name="filterChainDefinitions">
         <value>
            <!-- 配置登出: 使用 logout 过滤器 /shiro-logout = logout /shiro-* = anon /user.jsp
               = roles[user] /admin.jsp = roles[admin] /** = authc -->
            /index.jsp = anon
            /user/login =anon
            /logout = logout
            /user.jsp = roles[user]
            /admin.jsp = roles[admin]
            /** = authc
         </value>
      </property>
   </bean>
   <!--自定义拦截路径,可连接数据库-->
   <!--<bean id="filterChainDefinitionMap" factory-bean="filterChainDefinitionMapBuilder" factory-method="builderFilterChainDefinitionMap"></bean>
   <bean id="filterChainDefinitionMapBuilder" class="com.bzj.Factory.filterChainDefinitionMapBuilder"></bean>-->
</beans>

2)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"
      xmlns:aop="http://www.springframework.org/schema/aop"
      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-3.2.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

   <context:component-scan base-package="com.bzj">
      <context:exclude-filter type="annotation"
         expression="org.springframework.stereotype.Controller" />
   </context:component-scan>
   <aop:config proxy-target-class="true"/>
   
</beans>

3)applicationContext-serlvet.xml

(需要注意的是

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">

<property name="proxyTargetClass" value="true" /> </bean>

<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">

<property name="securityManager" ref="securityManager"/> </bean>

这两个配置文件需要加在SpringMvc配置文件当中,不然shiro在Controller的注解无法生效)

<?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"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            "
>
   <context:component-scan base-package="com.bzj" use-default-filters="false">
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
   </context:component-scan>
   <!-- 视图解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/"></property>
      <property name="suffix" value=".jsp"></property>
   </bean>

   <mvc:annotation-driven></mvc:annotation-driven>
   <mvc:default-servlet-handler/>

   <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
      <property name="proxyTargetClass" value="true" />
   </bean>
   <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
      <property name="securityManager" ref="securityManager"/>
   </bean>
</beans>

4)userRealm

package com.bzj.shiro;

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;


public class userRealm extends AuthorizingRealm {



    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        System.out.println("===============a");
        if("unknown".equals(username)){
            throw new UnknownAccountException("用户不才能在");
        }
        Object principal = username;
        Object credentials = null;
        if ("user".equals(username)){
            credentials = "098d2c478e9c11555ce2823231e02ec1";
        }else if ("admin".equals(username)){
            credentials = "038bdaf98f2037b31f1e75b5b4c9b26e";
        }
        String realName = getName();
        ByteSource salt = ByteSource.Util.bytes(username);
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, credentials, salt, realName);
        return info;


    }
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("==============b");
        Object principal = principalCollection.getPrimaryPrincipal();
        Set<String> roles = new HashSet<String>();
        Set<String> power = new HashSet<String>();
        roles.add("user");
        power.add("user:look");
        if ("admin".equals(principal)){
            roles.add("admin");
            power.add("admin:look");
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(roles);
        info.addStringPermissions(power);
        return info;
    }
    public static void main(String[]args){
        String hashAlgorithmName = "MD5";
        String credentials = "123456";
        int hashIterations = 1024;
        ByteSource credentialsSalt = ByteSource.Util.bytes("admin");
        Object obj = new SimpleHash(hashAlgorithmName, credentials, credentialsSalt, hashIterations);
        System.out.println(obj);
    }
}

5)LoginController

package com.bzj.controller;

import com.bzj.Service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/user")
public class LoginController {
    @Autowired
    UserService userService;
    @RequestMapping("login")
    public String login(@RequestParam String username,@RequestParam String password){
        Subject subject = SecurityUtils.getSubject();
        if(!subject.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            token.setRememberMe(true);
            try {
                subject.login(token);
            }catch (AuthenticationException e){
                System.out.println("=====");
            }
            System.out.println(subject.hasRole("user"));
            System.out.println(subject.hasRole("admin"));
        }
        return "success";

    }

    @RequiresRoles(value={"user"},logical = Logical.OR)
    @RequiresPermissions(value = {"user:look"},logical = Logical.OR)
    @RequestMapping("/testShiro")
    public String TestShiro(){
        userService.TestShiro();
        return "success";
    }

    @RequiresRoles(value={"admin"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:look"},logical = Logical.OR)
    @RequestMapping("/testShiroPermission")
    public String TestShiroPermission(){
        System.out.println("Permission!!=============");
        return "success";
    }

    @RequiresRoles(value={"admin"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:edit"},logical = Logical.OR)
    @RequestMapping("/TestShiroPermissionEdit")
    public String TestShiroPermissionEdit(){
        System.out.println("TestShiroPermissionEdit!!=============");
        return "success";
    }
}

6)Jsp页面:

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: beibei
  Date: 2018/11/2
  Time: 16:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>success!</h1>
<a href="/user.jsp">user</a>
<a href="/admin.jsp">admin</a>
<a href="/logout" >logout</a>
<a href="/user/testShiro">testShiro</a>
<a href="/user/testShiroPermission">testShiroPermission</a>
<a href="/user/TestShiroPermissionEdit">TestShiroPermissionEdit</a>
</body>
</html>

user.jsp

<%--
  Created by IntelliJ IDEA.
  User: beibei
  Date: 2018/11/3
  Time: 10:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

<html>
<head>
    <title>Title</title>
</head>
<body>
<shiro:hasRole name="user">拥有user角色</shiro:hasRole>
user
</body>
</html>

admin.jsp

<%--
  Created by IntelliJ IDEA.
  User: beibei
  Date: 2018/11/3
  Time: 10:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

<html>
<head>
    <title>Title</title>
</head>
<body>
<shiro:hasRole name="admin">拥有admin</shiro:hasRole>
</body>
</html>

unauthorizedUrl.jsp

<%--
  Created by IntelliJ IDEA.
  User: beibei
  Date: 2018/11/2
  Time: 16:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
unauthorizedUrl
</body>
</html>
发布了13 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_39701913/article/details/83713793