Shiro学习笔记1 —— Hello World

1、创建一个Maven工程加载Shiro的jar包

 1         <!-- junit -->
 2         <dependency>
 3             <groupId>junit</groupId>
 4             <artifactId>junit</artifactId>
 5             <version>4.12</version>
 6             <scope>test</scope>
 7         </dependency>
 8         <!-- Logging with SLF4J & LogBack -->
 9         <dependency>
10             <groupId>org.slf4j</groupId>
11             <artifactId>jcl-over-slf4j</artifactId>
12             <version>1.7.13</version>
13         </dependency>
14         <dependency>
15             <groupId>org.slf4j</groupId>
16             <artifactId>slf4j-api</artifactId>
17             <version>1.7.13</version>
18         </dependency>
19         <dependency>
20             <groupId>org.slf4j</groupId>
21             <artifactId>jul-to-slf4j</artifactId>
22             <version>1.7.13</version>
23         </dependency>
24         <dependency>
25             <groupId>ch.qos.logback</groupId>
26             <artifactId>logback-classic</artifactId>
27             <version>1.1.3</version>
28         </dependency>
29         <dependency>
30             <groupId>ch.qos.logback</groupId>
31             <artifactId>logback-core</artifactId>
32             <version>1.1.3</version>
33         </dependency>
34         <dependency>
35             <groupId>ch.qos.logback</groupId>
36             <artifactId>logback-access</artifactId>
37             <version>1.1.3</version>
38         </dependency>
39         <!-- shiro -->
40         <dependency>
41             <groupId>org.apache.shiro</groupId>
42             <artifactId>shiro-core</artifactId>
43             <version>1.2.4</version>
44         </dependency>
45         <!-- apache commons -->
46         <dependency>
47             <groupId>commons-logging</groupId>
48             <artifactId>commons-logging</artifactId>
49             <version>1.2</version>
50         </dependency>
View Code

2、日志文件:logback.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <configuration>
 3     <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
 4         <resetJUL>true</resetJUL>
 5     </contextListener>
 6     <!-- To enable JMX Management -->
 7     <jmxConfigurator />
 8 
 9     <!-- 打印到控制台 -->
10     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
11         <!-- encoder 默认配置为PatternLayoutEncoder -->
12         <layout class="ch.qos.logback.classic.PatternLayout">
13             <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</Pattern>
14         </layout>
15     </appender>
16 
17     <!-- 打印日志到文件 -->
18     <!-- 所有日志文件 -->
19     <appender name="AllLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
20         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
21             <fileNamePattern>log/shiro/all/%d{yyyy-MM-dd}.log</fileNamePattern>
22             <maxHistory>30</maxHistory>
23         </rollingPolicy>
24         <encoder>
25             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern>
26         </encoder>
27     </appender>
28     <!-- Task日志文件 -->
29     <appender name="Tasklog" class="ch.qos.logback.core.rolling.RollingFileAppender">
30         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
31             <fileNamePattern>log/shiro/task/%d{yyyy-MM-dd}.log</fileNamePattern>
32             <maxHistory>30</maxHistory>
33         </rollingPolicy>
34         <encoder>
35             <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern>
36         </encoder>
37     </appender>
38     
39     <!-- 为莫个包下的所有类指定Appender 这里也可以指定类名称例如:com.mcs.task.ClassName -->
40     <!-- 
41      <logger name="cn.mcs.task" level="DEBUG" >
42          <appender-ref ref="Tasklog" />
43      </logger>
44       -->
45      <logger name="org.springframework" level="INFO" /> 
46        <logger name="org.springframework.beans" level="DEBUG" />
47 
48     <!-- 常用级别:ERROR 、WARN 、INFO 、DEBUG 、TRACE -->
49     <root level="DEBUG">
50         <appender-ref ref="STDOUT" />
51         <!-- <appender-ref ref="AllLog" /> -->
52       </root>
53 
54 
55 </configuration>  
View Code

3、本地配置文件shiro.ini

1 [users]
2 admin=admin
3 zhang=123
4 wang=123
View Code

4、代码测试

 1 public class TestShiro {
 2     
 3     private static Logger log = LoggerFactory.getLogger(TestShiro.class);  
 4     
 5     @Test
 6     public void testHelloworld() {    
 7         //1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
 8         Factory<org.apache.shiro.mgt.SecurityManager> factory =
 9                 new IniSecurityManagerFactory("classpath:shiro.ini");
10         //2、得到SecurityManager实例 并绑定给SecurityUtils
11         org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
12         SecurityUtils.setSecurityManager(securityManager);
13         //3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)
14         Subject subject = SecurityUtils.getSubject();
15         UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");
16         
17         try {
18             //4、登录,即身份验证
19             subject.login(token);
20             log.info(token.getPrincipal().toString() + "用户登录验证成功!");
21         } catch (AuthenticationException e) {
22             //5、身份验证失败
23         }
24 
25         Assert.assertEquals(true, subject.isAuthenticated()); //断言用户已经登录
26 
27         //6、退出
28         subject.logout();
29         log.info("用户已成功注销!");
30     }
31 
32 }
View Code

 

posted @ 2015-12-11 11:23  小个子猫  阅读(653)  评论(0编辑  收藏  举报