nutzboot shiro配置 访问503
配置
##shiro
shiro.ini.urls:
shiro.url.login=/login
shiro.url.unauth=/login
/static = anon
/login = anon
/sys = authc
package io.nutz.nutzsite.module.sys.controllers;
import io.nutz.nutzsite.common.base.Result;
import io.nutz.nutzsite.common.exception.EmptyCaptchaException;
import io.nutz.nutzsite.common.exception.IncorrectCaptchaException;
import io.nutz.nutzsite.common.shiro.filter.AuthenticationFilter;
import io.nutz.nutzsite.module.sys.models.User;
import io.nutz.nutzsite.module.sys.services.UserService;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.integration.shiro.SimpleShiroToken;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@At("/login")
@IocBean
public class LoginController {
@Inject
private UserService userService;
@GET
@At({"","/login"})
@Ok("th:/login.html")
public void loginPage() {
}
@Ok("json")
@Fail("http:500")
@POST
@At("/login")
@Filters(@By(type = AuthenticationFilter.class))
public Result login(@Attr("loginToken") AuthenticationToken token, HttpServletRequest req, HttpSession session) {
int errCount = 0;
try {
//输错三次显示验证码窗口
errCount = NumberUtils.toInt(Strings.sNull(SecurityUtils.getSubject().getSession(true).getAttribute("errCount")));
Subject subject = SecurityUtils.getSubject();
ThreadContext.bind(subject);
subject.login(token);
User user = (User) subject.getPrincipal();
// int count = user.getLoginCount() == null ? 0 : user.getLoginCount();
userService.update(Chain.make("login_ip", user.getLoginIp()));
return Result.success("login.success");
} catch (IncorrectCaptchaException e) {
//自定义的验证码错误异常
return Result.error(1, "login.error.captcha");
} catch (EmptyCaptchaException e) {
//验证码为空
return Result.error(2, "login.error.captcha");
} catch (LockedAccountException e) {
return Result.error(3, "login.error.locked");
} catch (UnknownAccountException e) {
errCount++;
SecurityUtils.getSubject().getSession(true).setAttribute("errCount", errCount);
return Result.error(4, "login.error.user");
} catch (AuthenticationException e) {
errCount++;
SecurityUtils.getSubject().getSession(true).setAttribute("errCount", errCount);
return Result.error(5, "login.error.user");
} catch (Exception e) {
errCount++;
SecurityUtils.getSubject().getSession(true).setAttribute("errCount", errCount);
return Result.error(6, "login.error.system");
}
}
@Ok("th:/index.html")
@At
public void logout() {
Subject subject = SecurityUtils.getSubject();
if (subject.isAuthenticated()) {
subject.logout();
}
}
}
package io.nutz.nutzsite.common.shiro;
import io.nutz.nutzsite.common.exception.EmptyCaptchaException;
import io.nutz.nutzsite.common.exception.IncorrectCaptchaException;
import io.nutz.nutzsite.module.sys.models.User;
import io.nutz.nutzsite.module.sys.services.UserService;
import org.apache.commons.lang3.math.NumberUtils;
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.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.nutz.integration.shiro.AbstractSimpleAuthorizingRealm;
import org.nutz.integration.shiro.SimpleShiroToken;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import java.util.Set;
/**
* 自定义Realm 处理登录 权限
*/
@IocBean(name = "shiroRealm", fields = "dao")
public class UserRealm extends AbstractSimpleAuthorizingRealm {
@Inject
private UserService userService;
/**
* 授权
* @param principals
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// null usernames are invalid
if (principals == null) {
throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
}
String userId = String.valueOf(principals.getPrimaryPrincipal());
User user = dao().fetch(User.class, userId);
if (user == null) {
return null;
}
// 角色列表
Set<String> roles =userService.getRoleCodeList(user);
// 功能列表
Set<String> menus = userService.getMenuPermsList(user);
SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo();
auth.setRoles(roles);
auth.setStringPermissions(menus);
return auth;
}
/**
* 登录验证
* @param token
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
CaptchaToken authcToken = (CaptchaToken) token;
String loginname = authcToken.getUsername();
String captcha = authcToken.getCaptcha();
if (Strings.isBlank(loginname)) {
throw Lang.makeThrow(AuthenticationException.class, "Account name is empty");
}
User user = dao().fetch(User.class,loginname);
if (Lang.isEmpty(user)) {
throw Lang.makeThrow(UnknownAccountException.class, "Account [ %s ] not found", loginname);
}
int errCount = NumberUtils.toInt(Strings.sNull(SecurityUtils.getSubject().getSession(true).getAttribute("errCount")));
if (errCount > 2) {
//输错三次显示验证码窗口
if (Strings.isBlank(captcha)) {
throw Lang.makeThrow(EmptyCaptchaException.class, "Captcha is empty");
}
String _captcha = Strings.sBlank(SecurityUtils.getSubject().getSession(true).getAttribute("captcha"));
if (!authcToken.getCaptcha().equalsIgnoreCase(_captcha)) {
throw Lang.makeThrow(IncorrectCaptchaException.class, "Captcha is error");
}
}
if (user.isStatus()) {
throw Lang.makeThrow(LockedAccountException.class, "Account [ %s ] is locked.", loginname);
}
//设置验证码次数为零
SecurityUtils.getSubject().getSession(true).setAttribute("errCount", 0);
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName());
info.setCredentialsSalt(ByteSource.Util.bytes(user.getSalt()));
return info;
}
public UserRealm() {
this(null, null);
}
public UserRealm(CacheManager cacheManager, CredentialsMatcher matcher) {
super(cacheManager, matcher);
setAuthenticationTokenClass(SimpleShiroToken.class);
}
public UserRealm(CacheManager cacheManager) {
this(cacheManager, null);
}
public UserRealm(CredentialsMatcher matcher) {
this(null, matcher);
}
}
是我哪里配置不对吗
项目地址
https://github.com/HaimmingYu/ns