我是靠谱客的博主 欣喜老虎,最近开发中收集的这篇文章主要介绍Shiro简易实例:HelloWorld,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 1.程序整体布局:

2.新建工程/导入jar包(pom.xml)

<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>com.shiro</groupId>

       <artifactId>ShiroDemo01</artifactId>

       <version>0.0.1-SNAPSHOT</version>

       <build>

              <plugins>

                     <plugin>

                            <groupId>org.apache.maven.plugins</groupId>

                            <artifactId>maven-compiler-plugin</artifactId>

                            <version>3.1</version>

                            <configuration>

                                   <source>1.7</source>

                                   <target>1.7</target>

                            </configuration>

                     </plugin>

              </plugins>

       </build>

       <dependencies>

              <dependency>

                     <groupId>org.apache.shiro</groupId>

                     <artifactId>shiro-core</artifactId>

                     <version>1.3.2</version>

              </dependency>

              <dependency>

                     <groupId>org.slf4j</groupId>

                     <artifactId>slf4j-nop</artifactId>

                     <version>1.7.24</version>

              </dependency>

              <dependency>

    <groupId>log4j</groupId>

    <artifactId>log4j</artifactId>

    <version>1.2.17</version>

</dependency>

       </dependencies>

</project>

3.Log4j.properties

log4j.rootLogger=INFO, stdout

log4j.mylog=INFO, stdout

log4j.business=INFO, stdout

 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

 

# Keep 10 backup files.

log4j.appender.logfile.MaxBackupIndex=10

# Pattern to output: date priority [category] - message

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

4.shiro.ini

[users]

chx=123456

jack=12345

5.HelloWorld.java

package com.helloWorld;

 

import org.apache.log4j.Logger;

import org.apache.shiro.SecurityUtils;

import org.apache.shiro.authc.AuthenticationException;

import org.apache.shiro.authc.UsernamePasswordToken;

import org.apache.shiro.config.IniSecurityManagerFactory;

import org.apache.shiro.mgt.SecurityManager;

import org.apache.shiro.subject.Subject;

import org.apache.shiro.util.Factory;

 

 

public class HelloWorld {

       private static Logger logger = Logger.getLogger(HelloWorld.class);

 

       public static void main(String[] args) {

              // 1.读取配置文件,初始化Securityanager工厂

              Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");

              // 2.获取实例

              SecurityManager securityManager = factory.getInstance();

              // 3.SecurityManager实例绑定到SecurityUtils

              SecurityUtils.setSecurityManager(securityManager);

              // 4.得到当前执行的用户

              Subject subject = SecurityUtils.getSubject();

              // 5.创建token令牌,用户名/密码

              UsernamePasswordToken token = new UsernamePasswordToken("jack", "12345");

              // 6.身份认证

              try {

                     subject.login(token);

                     logger.info("登录成功!");

              } catch (AuthenticationException e) {

                     logger.info("登录失败!");

                     e.printStackTrace();

              }

              // 7.登出

              subject.logout();

       }

}

 

 

参考网址:

1. shiro入门实例: https://blog.csdn.net/qq_26525215/article/details/78059488

源码下载地址:https://download.csdn.net/download/qq_25337221/10762187

本内容由安康学院“雨季”原创!
 

 

 

 

 

 

 

 

最后

以上就是欣喜老虎为你收集整理的Shiro简易实例:HelloWorld的全部内容,希望文章能够帮你解决Shiro简易实例:HelloWorld所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部