Skip to content

Commit 8b13b1a

Browse files
committed
add shiro-tutorial demo 0\1\2
1 parent b8dd481 commit 8b13b1a

File tree

6 files changed

+300
-0
lines changed

6 files changed

+300
-0
lines changed

shiro-tutorial-1/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
<groupId>org.apache.shiro.tutorials</groupId>
8+
<artifactId>shiro-tutorial</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<name>First Apache Shiro Application</name>
11+
<packaging>jar</packaging>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<version>2.0.2</version>
23+
<configuration>
24+
<source>1.5</source>
25+
<target>1.5</target>
26+
<encoding>${project.build.sourceEncoding}</encoding>
27+
</configuration>
28+
</plugin>
29+
30+
<!-- This plugin is only to test run our little application. It is not
31+
needed in most Shiro-enabled applications: -->
32+
<plugin>
33+
<groupId>org.codehaus.mojo</groupId>
34+
<artifactId>exec-maven-plugin</artifactId>
35+
<version>1.1</version>
36+
<executions>
37+
<execution>
38+
<goals>
39+
<goal>java</goal>
40+
</goals>
41+
</execution>
42+
</executions>
43+
<configuration>
44+
<classpathScope>test</classpathScope>
45+
<mainClass>Tutorial</mainClass>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
51+
<dependencies>
52+
<dependency>
53+
<groupId>org.apache.shiro</groupId>
54+
<artifactId>shiro-core</artifactId>
55+
<version>1.1.0</version>
56+
</dependency>
57+
<!-- Shiro uses SLF4J for logging. We'll use the 'simple' binding
58+
in this example app. See http://www.slf4j.org for more info. -->
59+
<dependency>
60+
<groupId>org.slf4j</groupId>
61+
<artifactId>slf4j-simple</artifactId>
62+
<version>1.6.1</version>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
67+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import org.apache.shiro.SecurityUtils;
2+
import org.apache.shiro.authc.*;
3+
import org.apache.shiro.config.IniSecurityManagerFactory;
4+
import org.apache.shiro.mgt.SecurityManager;
5+
import org.apache.shiro.session.Session;
6+
import org.apache.shiro.subject.Subject;
7+
import org.apache.shiro.util.Factory;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
public class Tutorial {
12+
13+
private static final transient Logger log = LoggerFactory.getLogger(Tutorial.class);
14+
15+
public static void main(String[] args) {
16+
log.info("My First Apache Shiro Application");
17+
18+
19+
//1.
20+
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
21+
22+
//2.
23+
SecurityManager securityManager = factory.getInstance();
24+
25+
//3.
26+
SecurityUtils.setSecurityManager(securityManager);
27+
28+
System.exit(0);
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# =============================================================================
2+
# Tutorial INI configuration
3+
#
4+
# Usernames/passwords are based on the classic Mel Brooks' film "Spaceballs" :)
5+
# =============================================================================
6+
7+
# -----------------------------------------------------------------------------
8+
# Users and their (optional) assigned roles
9+
# username = password, role1, role2, ..., roleN
10+
# -----------------------------------------------------------------------------
11+
[users]
12+
root = secret, admin
13+
guest = guest, guest
14+
presidentskroob = 12345, president
15+
darkhelmet = ludicrousspeed, darklord, schwartz
16+
lonestarr = vespa, goodguy, schwartz
17+
18+
# -----------------------------------------------------------------------------
19+
# Roles with assigned permissions
20+
# roleName = perm1, perm2, ..., permN
21+
# -----------------------------------------------------------------------------
22+
[roles]
23+
admin = *
24+
schwartz = lightsaber:*
25+
goodguy = winnebago:drive:eagle5

shiro-tutorial-2/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
<groupId>org.apache.shiro.tutorials</groupId>
8+
<artifactId>shiro-tutorial</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<name>First Apache Shiro Application</name>
11+
<packaging>jar</packaging>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<version>2.0.2</version>
23+
<configuration>
24+
<source>1.5</source>
25+
<target>1.5</target>
26+
<encoding>${project.build.sourceEncoding}</encoding>
27+
</configuration>
28+
</plugin>
29+
30+
<!-- This plugin is only to test run our little application. It is not
31+
needed in most Shiro-enabled applications: -->
32+
<plugin>
33+
<groupId>org.codehaus.mojo</groupId>
34+
<artifactId>exec-maven-plugin</artifactId>
35+
<version>1.1</version>
36+
<executions>
37+
<execution>
38+
<goals>
39+
<goal>java</goal>
40+
</goals>
41+
</execution>
42+
</executions>
43+
<configuration>
44+
<classpathScope>test</classpathScope>
45+
<mainClass>Tutorial</mainClass>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
51+
<dependencies>
52+
<dependency>
53+
<groupId>org.apache.shiro</groupId>
54+
<artifactId>shiro-core</artifactId>
55+
<version>1.1.0</version>
56+
</dependency>
57+
<!-- Shiro uses SLF4J for logging. We'll use the 'simple' binding
58+
in this example app. See http://www.slf4j.org for more info. -->
59+
<dependency>
60+
<groupId>org.slf4j</groupId>
61+
<artifactId>slf4j-simple</artifactId>
62+
<version>1.6.1</version>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
67+
</project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import org.apache.shiro.SecurityUtils;
2+
import org.apache.shiro.authc.*;
3+
import org.apache.shiro.config.IniSecurityManagerFactory;
4+
import org.apache.shiro.mgt.SecurityManager;
5+
import org.apache.shiro.session.Session;
6+
import org.apache.shiro.subject.Subject;
7+
import org.apache.shiro.util.Factory;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
public class Tutorial {
12+
13+
private static final transient Logger log = LoggerFactory.getLogger(Tutorial.class);
14+
15+
16+
public static void main(String[] args) {
17+
log.info("My First Apache Shiro Application");
18+
19+
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
20+
SecurityManager securityManager = factory.getInstance();
21+
SecurityUtils.setSecurityManager(securityManager);
22+
23+
24+
// 获取当前执行用户:
25+
Subject currentUser = SecurityUtils.getSubject();
26+
27+
// 做点跟 Session 相关的事
28+
Session session = currentUser.getSession();
29+
session.setAttribute("someKey", "aValue");
30+
String value = (String) session.getAttribute("someKey");
31+
if (value.equals("aValue")) {
32+
log.info("Retrieved the correct value! [" + value + "]");
33+
}
34+
35+
// 登录当前用户检验角色和权限
36+
if (!currentUser.isAuthenticated()) {
37+
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
38+
token.setRememberMe(true);
39+
try {
40+
currentUser.login(token);
41+
} catch (UnknownAccountException uae) {
42+
log.info("There is no user with username of " + token.getPrincipal());
43+
} catch (IncorrectCredentialsException ice) {
44+
log.info("Password for account " + token.getPrincipal() + " was incorrect!");
45+
} catch (LockedAccountException lae) {
46+
log.info("The account for username " + token.getPrincipal() + " is locked. " +
47+
"Please contact your administrator to unlock it.");
48+
}
49+
// ... 捕获更多异常
50+
catch (AuthenticationException ae) {
51+
//无定义?错误?
52+
}
53+
}
54+
55+
//说出他们是谁:
56+
//打印主要识别信息 (本例是 username):
57+
log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
58+
59+
//测试角色:
60+
if (currentUser.hasRole("schwartz")) {
61+
log.info("May the Schwartz be with you!");
62+
} else {
63+
log.info("Hello, mere mortal.");
64+
}
65+
66+
//测试一个权限 (非(instance-level)实例级别)
67+
if (currentUser.isPermitted("lightsaber:weild")) {
68+
log.info("You may use a lightsaber ring. Use it wisely.");
69+
} else {
70+
log.info("Sorry, lightsaber rings are for schwartz masters only.");
71+
}
72+
73+
//一个(非常强大)的实例级别的权限:
74+
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
75+
log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
76+
"Here are the keys - have fun!");
77+
} else {
78+
log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
79+
}
80+
81+
//完成 - 退出t!
82+
currentUser.logout();
83+
84+
System.exit(0);
85+
}
86+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# =============================================================================
2+
# Tutorial INI configuration
3+
#
4+
# Usernames/passwords are based on the classic Mel Brooks' film "Spaceballs" :)
5+
# =============================================================================
6+
7+
# -----------------------------------------------------------------------------
8+
# Users and their (optional) assigned roles
9+
# username = password, role1, role2, ..., roleN
10+
# -----------------------------------------------------------------------------
11+
[users]
12+
root = secret, admin
13+
guest = guest, guest
14+
presidentskroob = 12345, president
15+
darkhelmet = ludicrousspeed, darklord, schwartz
16+
lonestarr = vespa, goodguy, schwartz
17+
18+
# -----------------------------------------------------------------------------
19+
# Roles with assigned permissions
20+
# roleName = perm1, perm2, ..., permN
21+
# -----------------------------------------------------------------------------
22+
[roles]
23+
admin = *
24+
schwartz = lightsaber:*
25+
goodguy = winnebago:drive:eagle5

0 commit comments

Comments
 (0)