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
825ea4d
commit 6ca2813
Showing
574 changed files
with
42,823 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
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,155 @@ | ||
<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/maven-v4_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-chapter18</artifactId> | ||
<packaging>war</packaging> | ||
<name>shiro-example-chapter18</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<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>commons-collections</groupId> | ||
<artifactId>commons-collections</artifactId> | ||
<version>3.2.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.shiro</groupId> | ||
<artifactId>shiro-core</artifactId> | ||
<version>1.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.shiro</groupId> | ||
<artifactId>shiro-ehcache</artifactId> | ||
<version>1.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.shiro</groupId> | ||
<artifactId>shiro-web</artifactId> | ||
<version>1.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.shiro</groupId> | ||
<artifactId>shiro-quartz</artifactId> | ||
<version>1.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.shiro</groupId> | ||
<artifactId>shiro-spring</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> | ||
|
||
<!-- aspectj相关jar包--> | ||
<dependency> | ||
<groupId>org.aspectj</groupId> | ||
<artifactId>aspectjrt</artifactId> | ||
<version>1.7.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.aspectj</groupId> | ||
<artifactId>aspectjweaver</artifactId> | ||
<version>1.7.4</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context-support</artifactId> | ||
<version>4.0.0.RELEASE</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-jdbc</artifactId> | ||
<version>4.0.0.RELEASE</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-tx</artifactId> | ||
<version>4.0.0.RELEASE</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<version>4.0.0.RELEASE</version> | ||
</dependency> | ||
|
||
|
||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.0.1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet.jsp</groupId> | ||
<artifactId>jsp-api</artifactId> | ||
<version>2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>jstl</artifactId> | ||
<version>1.2</version> | ||
</dependency> | ||
|
||
|
||
</dependencies> | ||
|
||
<build> | ||
<finalName>chapter18</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.mortbay.jetty</groupId> | ||
<artifactId>jetty-maven-plugin</artifactId> | ||
<version>8.1.8.v20121106</version> | ||
<configuration> | ||
<webAppConfig> | ||
<contextPath>/${project.build.finalName}</contextPath> | ||
</webAppConfig> | ||
</configuration> | ||
</plugin> | ||
|
||
|
||
<plugin> | ||
<groupId>org.apache.tomcat.maven</groupId> | ||
<artifactId>tomcat7-maven-plugin</artifactId> | ||
<version>2.2</version> | ||
<configuration> | ||
<path>/${project.build.finalName}</path> | ||
</configuration> | ||
|
||
</plugin> | ||
</plugins> | ||
|
||
|
||
</build> | ||
|
||
</project> |
10 changes: 10 additions & 0 deletions
10
shiro-example-chapter18/src/main/java/com/github/zhangkaitao/shiro/chapter18/Constants.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,10 @@ | ||
package com.github.zhangkaitao.shiro.chapter18; | ||
|
||
/** | ||
* <p>User: Zhang Kaitao | ||
* <p>Date: 14-2-15 | ||
* <p>Version: 1.0 | ||
*/ | ||
public class Constants { | ||
public static final String CURRENT_USER = "user"; | ||
} |
46 changes: 46 additions & 0 deletions
46
...om/github/zhangkaitao/shiro/chapter18/credentials/RetryLimitHashedCredentialsMatcher.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,46 @@ | ||
package com.github.zhangkaitao.shiro.chapter18.credentials; | ||
|
||
import org.apache.shiro.authc.AuthenticationInfo; | ||
import org.apache.shiro.authc.AuthenticationToken; | ||
import org.apache.shiro.authc.ExcessiveAttemptsException; | ||
import org.apache.shiro.authc.credential.HashedCredentialsMatcher; | ||
import org.apache.shiro.cache.Cache; | ||
import org.apache.shiro.cache.CacheManager; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* <p>User: Zhang Kaitao | ||
* <p>Date: 14-1-28 | ||
* <p>Version: 1.0 | ||
*/ | ||
public class RetryLimitHashedCredentialsMatcher extends HashedCredentialsMatcher { | ||
|
||
private Cache<String, AtomicInteger> passwordRetryCache; | ||
|
||
public RetryLimitHashedCredentialsMatcher(CacheManager cacheManager) { | ||
passwordRetryCache = cacheManager.getCache("passwordRetryCache"); | ||
} | ||
|
||
@Override | ||
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) { | ||
String username = (String)token.getPrincipal(); | ||
//retry count + 1 | ||
AtomicInteger retryCount = passwordRetryCache.get(username); | ||
if(retryCount == null) { | ||
retryCount = new AtomicInteger(0); | ||
passwordRetryCache.put(username, retryCount); | ||
} | ||
if(retryCount.incrementAndGet() > 5) { | ||
//if retry count > 5 throw | ||
throw new ExcessiveAttemptsException(); | ||
} | ||
|
||
boolean matches = super.doCredentialsMatch(token, info); | ||
if(matches) { | ||
//clear retry count | ||
passwordRetryCache.remove(username); | ||
} | ||
return matches; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...e-chapter18/src/main/java/com/github/zhangkaitao/shiro/chapter18/dao/OrganizationDao.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,24 @@ | ||
package com.github.zhangkaitao.shiro.chapter18.dao; | ||
|
||
import com.github.zhangkaitao.shiro.chapter18.entity.Organization; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* <p>Organization: Zhang Kaitao | ||
* <p>Date: 14-1-28 | ||
* <p>Version: 1.0 | ||
*/ | ||
public interface OrganizationDao { | ||
|
||
public Organization createOrganization(Organization organization); | ||
public Organization updateOrganization(Organization organization); | ||
public void deleteOrganization(Long organizationId); | ||
|
||
Organization findOne(Long organizationId); | ||
List<Organization> findAll(); | ||
|
||
List<Organization> findAllWithExclude(Organization excludeOraganization); | ||
|
||
void move(Organization source, Organization target); | ||
} |
95 changes: 95 additions & 0 deletions
95
...apter18/src/main/java/com/github/zhangkaitao/shiro/chapter18/dao/OrganizationDaoImpl.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,95 @@ | ||
package com.github.zhangkaitao.shiro.chapter18.dao; | ||
|
||
import com.github.zhangkaitao.shiro.chapter18.entity.Organization; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.PreparedStatementCreator; | ||
import org.springframework.jdbc.support.GeneratedKeyHolder; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
/** | ||
* <p>Organization: Zhang Kaitao | ||
* <p>Date: 14-1-28 | ||
* <p>Version: 1.0 | ||
*/ | ||
@Repository | ||
public class OrganizationDaoImpl implements OrganizationDao { | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
public Organization createOrganization(final Organization organization) { | ||
final String sql = "insert into sys_organization( name, parent_id, parent_ids, available) values(?,?,?,?)"; | ||
|
||
GeneratedKeyHolder keyHolder = new GeneratedKeyHolder(); | ||
jdbcTemplate.update(new PreparedStatementCreator() { | ||
@Override | ||
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { | ||
PreparedStatement psst = connection.prepareStatement(sql, new String[]{"id"}); | ||
int count = 1; | ||
psst.setString(count++, organization.getName()); | ||
psst.setLong(count++, organization.getParentId()); | ||
psst.setString(count++, organization.getParentIds()); | ||
psst.setBoolean(count++, organization.getAvailable()); | ||
return psst; | ||
} | ||
}, keyHolder); | ||
organization.setId(keyHolder.getKey().longValue()); | ||
return organization; | ||
} | ||
|
||
@Override | ||
public Organization updateOrganization(Organization organization) { | ||
final String sql = "update sys_organization set name=?, parent_id=?, parent_ids=?, available=? where id=?"; | ||
jdbcTemplate.update( | ||
sql, | ||
organization.getName(), organization.getParentId(), organization.getParentIds(), organization.getAvailable(), organization.getId()); | ||
return organization; | ||
} | ||
|
||
public void deleteOrganization(Long organizationId) { | ||
Organization organization = findOne(organizationId); | ||
final String deleteSelfSql = "delete from sys_organization where id=?"; | ||
jdbcTemplate.update(deleteSelfSql, organizationId); | ||
final String deleteDescendantsSql = "delete from sys_organization where parent_ids like ?"; | ||
jdbcTemplate.update(deleteDescendantsSql, organization.makeSelfAsParentIds() + "%"); | ||
} | ||
|
||
|
||
@Override | ||
public Organization findOne(Long organizationId) { | ||
final String sql = "select id, name, parent_id, parent_ids, available from sys_organization where id=?"; | ||
List<Organization> organizationList = jdbcTemplate.query(sql, new BeanPropertyRowMapper(Organization.class), organizationId); | ||
if(organizationList.size() == 0) { | ||
return null; | ||
} | ||
return organizationList.get(0); | ||
} | ||
|
||
@Override | ||
public List<Organization> findAll() { | ||
final String sql = "select id, name, parent_id, parent_ids, available from sys_organization"; | ||
return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Organization.class)); | ||
} | ||
|
||
@Override | ||
public List<Organization> findAllWithExclude(Organization excludeOraganization) { | ||
//TODO 改成not exists 利用索引 | ||
final String sql = "select id, name, parent_id, parent_ids, available from sys_organization where id!=? and parent_ids not like ?"; | ||
return jdbcTemplate.query(sql, new BeanPropertyRowMapper(Organization.class), excludeOraganization.getId(), excludeOraganization.makeSelfAsParentIds() + "%"); | ||
} | ||
|
||
@Override | ||
public void move(Organization source, Organization target) { | ||
String moveSourceSql = "update sys_organization set parent_id=?,parent_ids=? where id=?"; | ||
jdbcTemplate.update(moveSourceSql, target.getId(), target.getParentIds(), source.getId()); | ||
String moveSourceDescendantsSql = "update sys_organization set parent_ids=concat(?, substring(parent_ids, length(?))) where parent_ids like ?"; | ||
jdbcTemplate.update(moveSourceDescendantsSql, target.makeSelfAsParentIds(), source.makeSelfAsParentIds(), source.makeSelfAsParentIds() + "%"); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ample-chapter18/src/main/java/com/github/zhangkaitao/shiro/chapter18/dao/ResourceDao.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,21 @@ | ||
package com.github.zhangkaitao.shiro.chapter18.dao; | ||
|
||
import com.github.zhangkaitao.shiro.chapter18.entity.Resource; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* <p>Resource: Zhang Kaitao | ||
* <p>Date: 14-1-28 | ||
* <p>Version: 1.0 | ||
*/ | ||
public interface ResourceDao { | ||
|
||
public Resource createResource(Resource resource); | ||
public Resource updateResource(Resource resource); | ||
public void deleteResource(Long resourceId); | ||
|
||
Resource findOne(Long resourceId); | ||
List<Resource> findAll(); | ||
|
||
} |
Oops, something went wrong.