Skip to content

Commit

Permalink
第十九章
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangkaitao committed Feb 25, 2014
1 parent db8cea5 commit 4cce916
Show file tree
Hide file tree
Showing 585 changed files with 43,451 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<module>shiro-example-chapter17-server</module>
<module>shiro-example-chapter17-client</module>
<module>shiro-example-chapter18</module>
<module>shiro-example-chapter19</module>
</modules>


Expand Down
156 changes: 156 additions & 0 deletions shiro-example-chapter19/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<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-chapter19</artifactId>
<packaging>war</packaging>
<name>shiro-example-chapter19</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>chapter19</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.zhangkaitao.shiro.chapter19;

/**
* <p>User: Zhang Kaitao
* <p>Date: 14-2-15
* <p>Version: 1.0
*/
public class Constants {
public static final String CURRENT_USER = "user";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.zhangkaitao.shiro.chapter19.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.zhangkaitao.shiro.chapter19.dao;

import com.github.zhangkaitao.shiro.chapter19.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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.github.zhangkaitao.shiro.chapter19.dao;

import com.github.zhangkaitao.shiro.chapter19.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() + "%");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.zhangkaitao.shiro.chapter19.dao;

import com.github.zhangkaitao.shiro.chapter19.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();

}
Loading

0 comments on commit 4cce916

Please sign in to comment.