Shiro学习总结(二)--Shiro的入门小例子

前文提到shiro的一些概念,接下来开始shiro的实际操作:
Shiro的入门小例子:
第一步:配置jar包,这里采用maven的方式,故pom.xml的配置文件如下:
 
    
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.shiro</groupId>
  4. <artifactId>shiro-core</artifactId>
  5. <version>1.2.4</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.slf4j</groupId>
  9. <artifactId>slf4j-log4j12</artifactId>
  10. <version>1.7.12</version>
  11. </dependency>
第二步:暂时没有连接数据库,故采用静态数据,需要配置shiro.ini的文件模拟静态数据【user】表示用户,前面表示用户名
后面表示用户密码。
 
    
  1. [users]
  2. taojian=123456
  3. jack=123
第三步:创建log4j的属性文件,这个日志管理。
 
    
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. log4j.rootLogger=INFO, stdout
  20. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  21. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  22. log4j.appender.stdout.layout.ConversionPattern=%%[%c] - %%n
  23. # General Apache libraries
  24. log4j.logger.org.apache=WARN
  25. # Spring
  26. log4j.logger.org.springframework=WARN
  27. # Default Shiro logging
  28. log4j.logger.org.apache.shiro=TRACE
  29. # Disable verbose logging
  30. log4j.logger.org.apache.shiro.util.ThreadContext=WARN
  31. log4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN
第四步:编写关键的java代码;
 
    
  1. package com.shiro.hello;
  2. import org.apache.shiro.SecurityUtils;
  3. import org.apache.shiro.authc.AuthenticationException;
  4. import org.apache.shiro.authc.UsernamePasswordToken;
  5. import org.apache.shiro.config.IniSecurityManagerFactory;
  6. import org.apache.shiro.mgt.SecurityManager;
  7. import org.apache.shiro.subject.Subject;
  8. import org.apache.shiro.util.Factory;
  9. public class HelleWorld {
  10. public static void main(String[] args) {
  11. // 读取配置文件,初始化SecurityManager工厂
  12. Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro.ini");
  13. // 获取securityManager实例
  14. SecurityManager securityManager=factory.getInstance();
  15. // 把securityManager实例绑定到SecurityUtils
  16. SecurityUtils.setSecurityManager(securityManager);
  17. // 得到当前执行的用户
  18. Subject currentUser=SecurityUtils.getSubject();
  19. // 创建token令牌,用户名/密码
  20. UsernamePasswordToken token=new UsernamePasswordToken("taojian", "123456");
  21. try{
  22. // 身份认证
  23. currentUser.login(token);
  24. System.out.println("身份认证成功!");
  25. }catch(AuthenticationException e){
  26. e.printStackTrace();
  27. System.out.println("身份认证失败!");
  28. }
  29. // 退出
  30. currentUser.logout();
  31. }
  32. }

猜你喜欢

转载自blog.csdn.net/architect_csdn/article/details/80253051