Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1、重构privilege部分的代码。为shiro做准备,把shiro的基础类准备好 2、调整品类代码,包括数据库 3、调整配置文件位置及内容
- Loading branch information
Showing
23 changed files
with
260 additions
and
70 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
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
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
2 changes: 1 addition & 1 deletion
2
...a/band/wukong/mz/nutz/NutDruidFilter.java → ...utz/integration/druid/NutDruidFilter.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package band.wukong.mz.nutz; | ||
package band.wukong.mz.nutz.integration.druid; | ||
|
||
import org.nutz.mvc.NutFilter; | ||
|
||
|
115 changes: 115 additions & 0 deletions
115
src/main/java/band/wukong/mz/nutz/integration/shiro/NutzDaoRealm.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,115 @@ | ||
package band.wukong.mz.nutz.integration.shiro; | ||
|
||
import band.wukong.mz.common.privilege.bean.Permission; | ||
import band.wukong.mz.common.privilege.bean.Role; | ||
import band.wukong.mz.common.privilege.bean.User; | ||
import band.wukong.mz.util.Toolkit; | ||
import org.apache.shiro.SecurityUtils; | ||
import org.apache.shiro.authc.*; | ||
import org.apache.shiro.authc.credential.CredentialsMatcher; | ||
import org.apache.shiro.authz.AuthorizationException; | ||
import org.apache.shiro.authz.AuthorizationInfo; | ||
import org.apache.shiro.authz.SimpleAuthorizationInfo; | ||
import org.apache.shiro.cache.CacheManager; | ||
import org.apache.shiro.realm.AuthorizingRealm; | ||
import org.apache.shiro.subject.PrincipalCollection; | ||
import org.apache.shiro.util.ByteSource; | ||
import org.nutz.dao.Cnd; | ||
import org.nutz.dao.Dao; | ||
import org.nutz.integration.shiro.CaptchaUsernamePasswordToken; | ||
import org.nutz.ioc.loader.annotation.Inject; | ||
import org.nutz.lang.Strings; | ||
import org.nutz.mvc.Mvcs; | ||
|
||
/** | ||
* 用NutDao来实现Shiro的Realm | ||
* | ||
* @author wukong(wukonggg@139.com) | ||
*/ | ||
public class NutzDaoRealm extends AuthorizingRealm { | ||
|
||
@Inject | ||
private Dao dao; | ||
|
||
public NutzDaoRealm() { | ||
this(null, null); | ||
} | ||
|
||
public NutzDaoRealm(CacheManager cacheManager) { | ||
this(cacheManager, null); | ||
} | ||
|
||
public NutzDaoRealm(CredentialsMatcher matcher) { | ||
this(null, matcher); | ||
} | ||
|
||
public NutzDaoRealm(CacheManager cacheManager, CredentialsMatcher matcher) { | ||
super(cacheManager, matcher); | ||
setAuthenticationTokenClass(CaptchaUsernamePasswordToken.class); | ||
} | ||
|
||
public Dao dao() { | ||
if (dao == null) { | ||
dao = Mvcs.ctx().getDefaultIoc().get(Dao.class, "dao"); | ||
return dao; | ||
} | ||
return dao; | ||
} | ||
|
||
public void setDao(Dao dao) { | ||
this.dao = dao; | ||
} | ||
|
||
public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { | ||
//null usernames are invalid | ||
if (principals == null) { | ||
throw new AuthorizationException("PrincipalCollection method argument cannot be null."); | ||
} | ||
int userId = (Integer) principals.getPrimaryPrincipal(); | ||
User user = dao().fetch(User.class, userId); | ||
if (user == null) | ||
return null; | ||
if (user.isLocked()) | ||
throw new LockedAccountException("Account [" + user.getName() + "] is locked."); | ||
|
||
SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo(); | ||
user = dao().fetchLinks(user, null); | ||
if (user.getRoles() != null) { | ||
dao().fetchLinks(user.getRoles(), null); | ||
for (Role role : user.getRoles()) { | ||
auth.addRole(role.getName()); | ||
if (role.getPermissions() != null) { | ||
for (Permission p : role.getPermissions()) { | ||
auth.addStringPermission(p.getName()); | ||
} | ||
} | ||
} | ||
} | ||
if (user.getPermissions() != null) { | ||
for (Permission p : user.getPermissions()) { | ||
auth.addStringPermission(p.getName()); | ||
} | ||
} | ||
return auth; | ||
} | ||
|
||
public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { | ||
CaptchaUsernamePasswordToken upToken = (CaptchaUsernamePasswordToken) token; | ||
|
||
if (Strings.isBlank(upToken.getCaptcha())) | ||
throw new AuthenticationException("验证码不能为空"); | ||
String _captcha = Strings.sBlank(SecurityUtils.getSubject().getSession(true).getAttribute(Toolkit.captcha_attr)); | ||
if (!upToken.getCaptcha().equalsIgnoreCase(_captcha)) | ||
throw new AuthenticationException("验证码错误"); | ||
|
||
User user = dao().fetch(User.class, Cnd.where("name", "=", upToken.getUsername())); | ||
if (user == null) | ||
return null; | ||
if (user.isLocked()) | ||
throw new LockedAccountException("Account [" + upToken.getUsername() + "] is locked."); | ||
SimpleAccount account = new SimpleAccount(user.getId(), user.getPassword(), getName()); | ||
account.setCredentialsSalt(ByteSource.Util.bytes(user.getSalt())); | ||
return account; | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...a/band/wukong/mz/nutz/NutzTestHelper.java → ...d/wukong/mz/nutz/test/NutzTestHelper.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
Oops, something went wrong.