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
6c6a18e
commit 2940930
Showing
215 changed files
with
16,608 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...om/github/zhangkaitao/shiro/chapter20/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.chapter20.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; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...apter21/src/main/java/com/github/zhangkaitao/shiro/chapter20/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.chapter20.dao; | ||
|
||
import com.github.zhangkaitao.shiro.chapter20.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() + "%"); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...o-example-chapter21/src/main/java/com/github/zhangkaitao/shiro/chapter20/dao/RoleDao.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,20 @@ | ||
package com.github.zhangkaitao.shiro.chapter20.dao; | ||
|
||
import com.github.zhangkaitao.shiro.chapter20.entity.Role; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* <p>User: Zhang Kaitao | ||
* <p>Date: 14-1-28 | ||
* <p>Version: 1.0 | ||
*/ | ||
public interface RoleDao { | ||
|
||
public Role createRole(Role role); | ||
public Role updateRole(Role role); | ||
public void deleteRole(Long roleId); | ||
|
||
public Role findOne(Long roleId); | ||
public List<Role> findAll(); | ||
} |
24 changes: 24 additions & 0 deletions
24
...o-example-chapter21/src/main/java/com/github/zhangkaitao/shiro/chapter20/dao/UserDao.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.chapter20.dao; | ||
|
||
import com.github.zhangkaitao.shiro.chapter20.entity.User; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* <p>User: Zhang Kaitao | ||
* <p>Date: 14-1-28 | ||
* <p>Version: 1.0 | ||
*/ | ||
public interface UserDao { | ||
|
||
public User createUser(User user); | ||
public User updateUser(User user); | ||
public void deleteUser(Long userId); | ||
|
||
User findOne(Long userId); | ||
|
||
List<User> findAll(); | ||
|
||
User findByUsername(String username); | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
...ample-chapter21/src/main/java/com/github/zhangkaitao/shiro/chapter20/dao/UserDaoImpl.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,89 @@ | ||
package com.github.zhangkaitao.shiro.chapter20.dao; | ||
|
||
import com.github.zhangkaitao.shiro.chapter20.entity.User; | ||
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>User: Zhang Kaitao | ||
* <p>Date: 14-1-28 | ||
* <p>Version: 1.0 | ||
*/ | ||
@Repository | ||
public class UserDaoImpl implements UserDao { | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
public User createUser(final User user) { | ||
final String sql = "insert into sys_user(organization_id, username, password, salt, role_ids, locked) 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.setLong(count++, user.getOrganizationId()); | ||
psst.setString(count++, user.getUsername()); | ||
psst.setString(count++, user.getPassword()); | ||
psst.setString(count++, user.getSalt()); | ||
psst.setString(count++, user.getRoleIdsStr()); | ||
psst.setBoolean(count++, user.getLocked()); | ||
return psst; | ||
} | ||
}, keyHolder); | ||
|
||
user.setId(keyHolder.getKey().longValue()); | ||
return user; | ||
} | ||
|
||
public User updateUser(User user) { | ||
String sql = "update sys_user set organization_id=?,username=?, password=?, salt=?, role_ids=?, locked=? where id=?"; | ||
jdbcTemplate.update( | ||
sql, | ||
user.getOrganizationId(), user.getUsername(), user.getPassword(), user.getSalt(), user.getRoleIdsStr(), user.getLocked(), user.getId()); | ||
return user; | ||
} | ||
|
||
public void deleteUser(Long userId) { | ||
String sql = "delete from sys_user where id=?"; | ||
jdbcTemplate.update(sql, userId); | ||
} | ||
|
||
@Override | ||
public User findOne(Long userId) { | ||
String sql = "select id, organization_id, username, password, salt, role_ids as roleIdsStr, locked from sys_user where id=?"; | ||
List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class), userId); | ||
if(userList.size() == 0) { | ||
return null; | ||
} | ||
return userList.get(0); | ||
} | ||
|
||
@Override | ||
public List<User> findAll() { | ||
String sql = "select id, organization_id, username, password, salt, role_ids as roleIdsStr, locked from sys_user"; | ||
return jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class)); | ||
} | ||
|
||
|
||
@Override | ||
public User findByUsername(String username) { | ||
String sql = "select id, organization_id, username, password, salt, role_ids as roleIdsStr, locked from sys_user where username=?"; | ||
List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper(User.class), username); | ||
if(userList.size() == 0) { | ||
return null; | ||
} | ||
return userList.get(0); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...mple-chapter21/src/main/java/com/github/zhangkaitao/shiro/chapter20/dao/UserRunAsDao.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,20 @@ | ||
package com.github.zhangkaitao.shiro.chapter20.dao; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* <p>User: Zhang Kaitao | ||
* <p>Date: 14-1-28 | ||
* <p>Version: 1.0 | ||
*/ | ||
public interface UserRunAsDao { | ||
|
||
public void grantRunAs(Long fromUserId, Long toUserId); | ||
public void revokeRunAs(Long fromUserId, Long toUserId); | ||
|
||
public boolean exists(Long fromUserId, Long toUserId); | ||
|
||
public List<Long> findFromUserIds(Long toUserId); | ||
public List<Long> findToUserIds(Long fromUserId); | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
...e-chapter21/src/main/java/com/github/zhangkaitao/shiro/chapter20/entity/Organization.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,93 @@ | ||
package com.github.zhangkaitao.shiro.chapter20.entity; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* <p>User: Zhang Kaitao | ||
* <p>Date: 14-1-28 | ||
* <p>Version: 1.0 | ||
*/ | ||
public class Organization implements Serializable { | ||
private Long id; //编号 | ||
private String name; //组织机构名称 | ||
private Long parentId; //父编号 | ||
private String parentIds; //父编号列表,如1/2/ | ||
private Boolean available = Boolean.FALSE; | ||
|
||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Long getParentId() { | ||
return parentId; | ||
} | ||
|
||
public void setParentId(Long parentId) { | ||
this.parentId = parentId; | ||
} | ||
|
||
public String getParentIds() { | ||
return parentIds; | ||
} | ||
|
||
public void setParentIds(String parentIds) { | ||
this.parentIds = parentIds; | ||
} | ||
|
||
public Boolean getAvailable() { | ||
return available; | ||
} | ||
|
||
public void setAvailable(Boolean available) { | ||
this.available = available; | ||
} | ||
|
||
public boolean isRootNode() { | ||
return parentId == 0; | ||
} | ||
|
||
public String makeSelfAsParentIds() { | ||
return getParentIds() + getId() + "/"; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
Organization that = (Organization) o; | ||
|
||
if (id != null ? !id.equals(that.id) : that.id != null) return false; | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return id != null ? id.hashCode() : 0; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Organization{" + | ||
"id=" + id + | ||
", name='" + name + '\'' + | ||
", parentId=" + parentId + | ||
", parentIds='" + parentIds + '\'' + | ||
", available=" + available + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.