• shiro的简单入门使用


    这里只是测试登录认证,没有web模块,没有连接数据库,用户密码放在shiro.ini配置中,密码没有加密处理,简单入门。

    基于maven

    先看目录结构

    测试结果

    pom.xml

      1 <?xml version="1.0" encoding="UTF-8"?>
      2 
      3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      5   <modelVersion>4.0.0</modelVersion>
      6 
      7   <groupId>com.xiaostudy</groupId>
      8   <artifactId>shiro_test2</artifactId>
      9   <version>1.0-SNAPSHOT</version>
     10   <packaging>war</packaging>
     11 
     12   <name>shiro_test2 Maven Webapp</name>
     13   <!-- FIXME change it to the project's website -->
     14   <url>http://www.example.com</url>
     15 
     16   <properties>
     17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     18     <maven.compiler.source>1.7</maven.compiler.source>
     19     <maven.compiler.target>1.7</maven.compiler.target>
     20     <shiro.version>1.3.0</shiro.version>
     21     <quartz.version>2.2.3</quartz.version>
     22     <cas.client.version>3.1.9</cas.client.version>
     23     <junit.version>4.12</junit.version>
     24     <slf4j.version>1.7.7</slf4j.version>
     25     <log4j.version>1.2.17</log4j.version>
     26   </properties>
     27 
     28   <dependencies>
     29     <!-- shiro包开始 -->
     30     <dependency>
     31       <groupId>org.apache.shiro</groupId>
     32       <artifactId>shiro-ehcache</artifactId>
     33       <version>${shiro.version}</version>
     34     </dependency>
     35     <dependency>
     36       <groupId>org.apache.shiro</groupId>
     37       <artifactId>shiro-core</artifactId>
     38       <version>${shiro.version}</version>
     39     </dependency>
     40     <dependency>
     41       <groupId>org.apache.shiro</groupId>
     42       <artifactId>shiro-web</artifactId>
     43       <version>${shiro.version}</version>
     44     </dependency>
     45     <dependency>
     46       <groupId>org.apache.shiro</groupId>
     47       <artifactId>shiro-spring</artifactId>
     48       <version>${shiro.version}</version>
     49     </dependency>
     50     <dependency>
     51       <groupId>org.quartz-scheduler</groupId>
     52       <artifactId>quartz</artifactId>
     53       <version>${quartz.version}</version>
     54       <exclusions>
     55         <exclusion>
     56           <artifactId>c3p0</artifactId>
     57           <groupId>c3p0</groupId>
     58         </exclusion>
     59       </exclusions>
     60     </dependency>
     61     <dependency>
     62       <groupId>org.apache.shiro</groupId>
     63       <artifactId>shiro-cas</artifactId>
     64       <version>${shiro.version}</version>
     65     </dependency>
     66     <dependency>
     67       <groupId>org.jasig.cas</groupId>
     68       <artifactId>cas-client-core</artifactId>
     69       <version>${cas.client.version}</version>
     70     </dependency>
     71     <!-- shiro 包结束 -->
     72 
     73     <!--测试-->
     74     <dependency>
     75       <groupId>junit</groupId>
     76       <artifactId>junit</artifactId>
     77       <version>${junit.version}</version>
     78       <scope>test</scope>
     79     </dependency>
     80 
     81     <!-- 日志文件管理包 -->
     82     <dependency>
     83       <groupId>log4j</groupId>
     84       <artifactId>log4j</artifactId>
     85       <version>${log4j.version}</version>
     86     </dependency>
     87     <dependency>
     88       <groupId>org.slf4j</groupId>
     89       <artifactId>slf4j-api</artifactId>
     90       <version>${slf4j.version}</version>
     91     </dependency>
     92     <dependency>
     93       <groupId>org.slf4j</groupId>
     94       <artifactId>slf4j-log4j12</artifactId>
     95       <version>${slf4j.version}</version>
     96     </dependency>
     97 
     98     <dependency>
     99       <groupId>junit</groupId>
    100       <artifactId>junit</artifactId>
    101       <version>4.11</version>
    102       <scope>test</scope>
    103     </dependency>
    104 
    105     <dependency>
    106       <groupId>org.junit.jupiter</groupId>
    107       <artifactId>junit-jupiter-api</artifactId>
    108       <version>RELEASE</version>
    109       <scope>compile</scope>
    110     </dependency>
    111   </dependencies>
    112 
    113   <build>
    114     <finalName>shiro_test2</finalName>
    115     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
    116       <plugins>
    117         <plugin>
    118           <artifactId>maven-clean-plugin</artifactId>
    119           <version>3.1.0</version>
    120         </plugin>
    121         <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
    122         <plugin>
    123           <artifactId>maven-resources-plugin</artifactId>
    124           <version>3.0.2</version>
    125         </plugin>
    126         <plugin>
    127           <artifactId>maven-compiler-plugin</artifactId>
    128           <version>3.8.0</version>
    129         </plugin>
    130         <plugin>
    131           <artifactId>maven-surefire-plugin</artifactId>
    132           <version>2.22.1</version>
    133         </plugin>
    134         <plugin>
    135           <artifactId>maven-war-plugin</artifactId>
    136           <version>3.2.2</version>
    137         </plugin>
    138         <plugin>
    139           <artifactId>maven-install-plugin</artifactId>
    140           <version>2.5.2</version>
    141         </plugin>
    142         <plugin>
    143           <artifactId>maven-deploy-plugin</artifactId>
    144           <version>2.8.2</version>
    145         </plugin>
    146       </plugins>
    147     </pluginManagement>
    148   </build>
    149 </project>

     shiro.ini

    1 #对用户信息进行配置
    2 [users]
    3 #用户账号和密码
    4 zhangsan=123456
    5 lisi=654321

    log4j.properties

    1 log4j.rootLogger=debug, stdout
    2 
    3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    5 log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n

    测试代码

     1 package com.xiaostudy.test;
     2 
     3 import org.apache.shiro.SecurityUtils;
     4 import org.apache.shiro.authc.AuthenticationException;
     5 import org.apache.shiro.authc.UsernamePasswordToken;
     6 import org.apache.shiro.config.IniSecurityManagerFactory;
     7 import org.apache.shiro.mgt.DefaultSecurityManager;
     8 import org.apache.shiro.realm.text.IniRealm;
     9 import org.apache.shiro.subject.Subject;
    10 import org.apache.shiro.util.Factory;
    11 import org.junit.jupiter.api.Test;
    12 
    13 /**
    14  * Created with IntelliJ IDEA.
    15  * @author xiaostudy
    16  * Date: 2019/4/29
    17  * Time: 10:20
    18  * Description: No Description
    19  */
    20 public class Test1 {
    21 
    22     // 用户登录和退出
    23     @Test
    24     public void testLoginAndLogout() {
    25 
    26         DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
    27         IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
    28         defaultSecurityManager.setRealm(iniRealm);
    29         SecurityUtils.setSecurityManager(defaultSecurityManager);
    30 
    31         // 从SecurityUtils中创建一个subject  (org.apache.shiro.subject.Subject) 即主体
    32         Subject subject = SecurityUtils.getSubject();
    33 
    34         // 在认证提交前准备token(令牌)   这里的账号和密码 将来是由用户输入进去的
    35         UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "123456");
    36 
    37         //执行认证提交
    38         try {
    39             subject.login(token);
    40         } catch (AuthenticationException e) {
    41             e.printStackTrace();
    42         }
    43 
    44 
    45         // 是否认证通过 (org.apache.shiro.authc.Authenticator) 即认证器
    46         boolean isAuthenticated = subject.isAuthenticated();
    47         System.out.println("是否认证通过:"+isAuthenticated);
    48         if(isAuthenticated) {
    49             System.out.println("subject:" + subject);
    50             System.out.println("用户:" + subject.getPrincipal());
    51             System.out.println("getPrincipal().getClass(): " + subject.getPrincipal().getClass());
    52         }
    53 
    54         //退出操作
    55         subject.logout();
    56 
    57         // 是否认证通过
    58         isAuthenticated = subject.isAuthenticated();
    59         System.out.println("是否认证通过:"+isAuthenticated);
    60     }
    61 }
  • 相关阅读:
    Hibernate缓存机制
    如何建立索引
    数据库索引及基本优化入门
    索引优化-2
    索引优化-1
    Linux基本命令参数
    Spring 依赖注入(控制反转)介绍
    Linux的软连接和硬连接
    聚簇索引(Clustered Index)和非聚簇索引 (Non- Clustered Index)
    常见和链表相关的算法
  • 原文地址:https://www.cnblogs.com/xiaostudy/p/10789248.html
Copyright © 2020-2023  润新知