forked from zhangkaitao/shiro-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9cce13d
commit f6a1e12
Showing
8 changed files
with
357 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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"> | ||
<parent> | ||
<artifactId>shiro-example</artifactId> | ||
<groupId>com.github.zhangkaitao</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>shiro-example-chapter4</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.9</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
<version>1.1.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.shiro</groupId> | ||
<artifactId>shiro-core</artifactId> | ||
<version>1.2.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.25</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid</artifactId> | ||
<version>0.2.23</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
|
||
</project> |
49 changes: 49 additions & 0 deletions
49
...chapter4/src/test/java/com/github/zhangkaitao/shiro/chapter4/ConfigurationCreateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.zhangkaitao.shiro.chapter4; | ||
|
||
import com.alibaba.druid.pool.DruidDataSource; | ||
import junit.framework.Assert; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.authc.UsernamePasswordToken; | ||
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy; | ||
import org.apache.shiro.authc.pam.ModularRealmAuthenticator; | ||
import org.apache.shiro.authz.ModularRealmAuthorizer; | ||
import org.apache.shiro.authz.permission.WildcardPermissionResolver; | ||
import org.apache.shiro.config.IniSecurityManagerFactory; | ||
import org.apache.shiro.mgt.DefaultSecurityManager; | ||
import org.apache.shiro.realm.Realm; | ||
import org.apache.shiro.realm.jdbc.JdbcRealm; | ||
import org.apache.shiro.subject.Subject; | ||
import org.apache.shiro.util.Factory; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* <p>User: Zhang Kaitao | ||
* <p>Date: 14-1-27 | ||
* <p>Version: 1.0 | ||
*/ | ||
public class ConfigurationCreateTest { | ||
|
||
@Test | ||
public void test() { | ||
|
||
Factory<org.apache.shiro.mgt.SecurityManager> factory = | ||
new IniSecurityManagerFactory("classpath:shiro-config.ini"); | ||
|
||
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); | ||
|
||
//将SecurityManager设置到SecurityUtils 方便全局使用 | ||
SecurityUtils.setSecurityManager(securityManager); | ||
|
||
Subject subject = SecurityUtils.getSubject(); | ||
|
||
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123"); | ||
subject.login(token); | ||
|
||
Assert.assertTrue(subject.isAuthenticated()); | ||
|
||
|
||
|
||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
shiro-example-chapter4/src/test/java/com/github/zhangkaitao/shiro/chapter4/IniMainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.github.zhangkaitao.shiro.chapter4; | ||
|
||
import com.alibaba.druid.pool.DruidDataSource; | ||
import junit.framework.Assert; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.authc.UsernamePasswordToken; | ||
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy; | ||
import org.apache.shiro.authc.pam.ModularRealmAuthenticator; | ||
import org.apache.shiro.authz.ModularRealmAuthorizer; | ||
import org.apache.shiro.authz.permission.WildcardPermissionResolver; | ||
import org.apache.shiro.codec.Hex; | ||
import org.apache.shiro.config.IniSecurityManagerFactory; | ||
import org.apache.shiro.mgt.*; | ||
import org.apache.shiro.realm.Realm; | ||
import org.apache.shiro.realm.jdbc.JdbcRealm; | ||
import org.apache.shiro.subject.Subject; | ||
import org.apache.shiro.util.Factory; | ||
import org.junit.Test; | ||
import sun.misc.BASE64Encoder; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* <p>User: Zhang Kaitao | ||
* <p>Date: 14-1-27 | ||
* <p>Version: 1.0 | ||
*/ | ||
public class IniMainTest { | ||
|
||
@Test | ||
public void test() { | ||
|
||
Factory<org.apache.shiro.mgt.SecurityManager> factory = | ||
new IniSecurityManagerFactory("classpath:shiro-config-main.ini"); | ||
|
||
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); | ||
|
||
//将SecurityManager设置到SecurityUtils 方便全局使用 | ||
SecurityUtils.setSecurityManager(securityManager); | ||
|
||
Subject subject = SecurityUtils.getSubject(); | ||
|
||
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123"); | ||
subject.login(token); | ||
|
||
Assert.assertTrue(subject.isAuthenticated()); | ||
|
||
|
||
|
||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...pter4/src/test/java/com/github/zhangkaitao/shiro/chapter4/NonConfigurationCreateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.github.zhangkaitao.shiro.chapter4; | ||
|
||
import com.alibaba.druid.pool.DruidDataSource; | ||
import junit.framework.Assert; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.authc.UsernamePasswordToken; | ||
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy; | ||
import org.apache.shiro.authc.pam.ModularRealmAuthenticator; | ||
import org.apache.shiro.authz.ModularRealmAuthorizer; | ||
import org.apache.shiro.authz.permission.WildcardPermissionResolver; | ||
import org.apache.shiro.mgt.DefaultSecurityManager; | ||
import org.apache.shiro.realm.Realm; | ||
import org.apache.shiro.realm.jdbc.JdbcRealm; | ||
import org.apache.shiro.realm.text.IniRealm; | ||
import org.apache.shiro.subject.Subject; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* <p>User: Zhang Kaitao | ||
* <p>Date: 14-1-27 | ||
* <p>Version: 1.0 | ||
*/ | ||
public class NonConfigurationCreateTest { | ||
|
||
@Test | ||
public void test() { | ||
|
||
DefaultSecurityManager securityManager = new DefaultSecurityManager(); | ||
|
||
//设置authenticator | ||
ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator(); | ||
authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy()); | ||
securityManager.setAuthenticator(authenticator); | ||
|
||
//设置authorizer | ||
ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer(); | ||
authorizer.setPermissionResolver(new WildcardPermissionResolver()); | ||
securityManager.setAuthorizer(authorizer); | ||
|
||
//设置Realm | ||
DruidDataSource ds = new DruidDataSource(); | ||
ds.setDriverClassName("com.mysql.jdbc.Driver"); | ||
ds.setUrl("jdbc:mysql://localhost:3306/shiro"); | ||
ds.setUsername("root"); | ||
ds.setPassword(""); | ||
|
||
JdbcRealm jdbcRealm = new JdbcRealm(); | ||
jdbcRealm.setDataSource(ds); | ||
jdbcRealm.setPermissionsLookupEnabled(true); | ||
securityManager.setRealms(Arrays.asList((Realm) jdbcRealm)); | ||
|
||
//将SecurityManager设置到SecurityUtils 方便全局使用 | ||
SecurityUtils.setSecurityManager(securityManager); | ||
|
||
Subject subject = SecurityUtils.getSubject(); | ||
|
||
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123"); | ||
subject.login(token); | ||
|
||
Assert.assertTrue(subject.isAuthenticated()); | ||
|
||
|
||
|
||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...r4/src/test/java/com/github/zhangkaitao/shiro/chapter4/authenticator/MyAuthenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.github.zhangkaitao.shiro.chapter4.authenticator; | ||
|
||
import org.apache.shiro.authc.pam.ModularRealmAuthenticator; | ||
import org.apache.shiro.realm.Realm; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* <p>User: Zhang Kaitao | ||
* <p>Date: 14-1-27 | ||
* <p>Version: 1.0 | ||
*/ | ||
public class MyAuthenticator extends ModularRealmAuthenticator { | ||
|
||
public void setBytes(byte[] bytes) { | ||
System.out.println(new String(bytes)); | ||
} | ||
|
||
public void setArray(int[] ints) { | ||
System.out.println(Arrays.toString(ints)); | ||
} | ||
|
||
public void setSet(Set<Realm> realms) { | ||
System.out.println(realms); | ||
} | ||
|
||
public void setMap(Map<Object, Object> maps) { | ||
System.out.println(maps); | ||
System.out.println(maps.get("1")); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
shiro-example-chapter4/src/test/resources/shiro-config-main.ini
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[main] | ||
securityManager=org.apache.shiro.mgt.DefaultSecurityManager | ||
|
||
#authenticator | ||
authenticator=com.github.zhangkaitao.shiro.chapter4.authenticator.MyAuthenticator | ||
|
||
#base64 byte[] | ||
authenticator.bytes=aGVsbG8= | ||
#hex byte[] | ||
authenticator.bytes=0x68656c6c6f | ||
|
||
authenticator.array=1,2,3 | ||
authenticator.set=$jdbcRealm,$jdbcRealm | ||
authenticator.map=$jdbcRealm:$jdbcRealm,1:1,key:abc | ||
|
||
authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy | ||
authenticator.authenticationStrategy=$authenticationStrategy | ||
securityManager.authenticator=$authenticator | ||
|
||
#authorizer | ||
authorizer=org.apache.shiro.authz.ModularRealmAuthorizer | ||
permissionResolver=org.apache.shiro.authz.permission.WildcardPermissionResolver | ||
authorizer.permissionResolver=$permissionResolver | ||
securityManager.authorizer=$authorizer | ||
|
||
#realm | ||
dataSource=com.alibaba.druid.pool.DruidDataSource | ||
dataSource.driverClassName=com.mysql.jdbc.Driver | ||
dataSource.url=jdbc:mysql://localhost:3306/shiro | ||
dataSource.username=root | ||
#dataSource.password= | ||
|
||
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm | ||
jdbcRealm.dataSource=$dataSource | ||
jdbcRealm.permissionsLookupEnabled=true | ||
securityManager.realms=$jdbcRealm |
27 changes: 27 additions & 0 deletions
27
shiro-example-chapter4/src/test/resources/shiro-config.ini
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[main] | ||
#覆盖默认的securityManager | ||
#securityManager=org.apache.shiro.mgt.DefaultSecurityManager | ||
|
||
#authenticator | ||
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator | ||
authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy | ||
authenticator.authenticationStrategy=$authenticationStrategy | ||
securityManager.authenticator=$authenticator | ||
|
||
#authorizer | ||
authorizer=org.apache.shiro.authz.ModularRealmAuthorizer | ||
permissionResolver=org.apache.shiro.authz.permission.WildcardPermissionResolver | ||
authorizer.permissionResolver=$permissionResolver | ||
securityManager.authorizer=$authorizer | ||
|
||
#realm | ||
dataSource=com.alibaba.druid.pool.DruidDataSource | ||
dataSource.driverClassName=com.mysql.jdbc.Driver | ||
dataSource.url=jdbc:mysql://localhost:3306/shiro | ||
dataSource.username=root | ||
#dataSource.password= | ||
|
||
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm | ||
jdbcRealm.dataSource=$dataSource | ||
jdbcRealm.permissionsLookupEnabled=true | ||
securityManager.realms=$jdbcRealm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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"> | ||
<parent> | ||
<artifactId>shiro-example</artifactId> | ||
<groupId>com.github.zhangkaitao</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>shiro-example-chapter5</artifactId> | ||
|
||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.9</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
<version>1.1.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.shiro</groupId> | ||
<artifactId>shiro-core</artifactId> | ||
<version>1.2.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.25</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid</artifactId> | ||
<version>0.2.23</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
|
||
</project> |