<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" >
<property name= "securityManager" ref="securityManager" />
<property name= "loginUrl" value ="/login.action" />
<property name= "successUrl" value="/first.action" />
<property name= "unauthorizedUrl" value="/refuse.jsp" />
<property name= "filters">
<map>
<entry key= "authc" value-ref="formAuthenticationFilter" />
map>
property>
<property name= "filterChainDefinitions" >
<value>
/images/** = anon
/ js/** = anon
/styles/** = anon
/ jfreechart = anon
/test/** =anon
/validatecode.jsp = anon
/logout.action = logout
/** = authc
value>
property>
bean >
|
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
if (isLoginRequest(request, response)) {
if (isLoginSubmission(request, response)) {
if (log .isTraceEnabled()) {
log.trace("Login submission detected. Attempting to execute login.");
}
return executeLogin(request, response);
} else {
if (log .isTraceEnabled()) {
log.trace("Login page view." );
}
//allow them to see the login page ;)
return true ;
}
} else {
if (log .isTraceEnabled()) {
log.trace("Attempting to access a path which requires authentication. Forwarding to the " +
"Authentication url [" + getLoginUrl() + "]" );
}
saveRequestAndRedirectToLogin(request, response);
return false ;
}
|
public class customRealm extends AuthorizingRealm{
@Override
public boolean supports(AuthenticationToken token) {
return token instanceof UsernamePasswordToken;
}
@Override
public String getName() {
return "customRealm" ;
}
@Autowired
private ServiceFacade serviceFacade;
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
//首先获取用户输入的账号。根据账号去数据库中查用户。对用户做出判断
String usercode = (String) token.getPrincipal();
//根据 usercode查询用户
SysUser sysUser = serviceFacade.getSysUserService().findSysUserByUserCode(usercode);
if (sysUser==null) {
return null ;
}
String pwd = sysUser.getPwd();
ActiveUser activeUser = serviceFacade.getSysUserService().createUser(usercode);
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(activeUser,pwd, getName());
return simpleAuthenticationInfo;
}
|
public String login() {
HttpServletRequest request = this.getRequest();
// shiro 在认证过程中出现错误后将异常类路径通过request返回
String exceptionClassName = (String) request
.getAttribute( "shiroLoginFailure");
if (exceptionClassName != null) {
if (UnknownAccountException.class .getName().equals(
exceptionClassName)) {
// 账号不存在
// 抛出异常
ResultInfo resultInfo = ResultUtil.createFail(Config.MESSAGE,
101, null);
throw new ExceptionResultInfo(resultInfo);
} else if (IncorrectCredentialsException.class .getName().equals(
exceptionClassName)) {
// 用户名或密码 错误
ResultInfo resultInfo = ResultUtil.createFail(Config.MESSAGE,
114, null);
throw new ExceptionResultInfo(resultInfo);
} else if ("randomCodeError" .equals(exceptionClassName)) {
// 提供验证码错误
ResultInfo resultInfo = ResultUtil.createFail(Config.MESSAGE,
113, null);
throw new ExceptionResultInfo(resultInfo);
} else {
// 最终在异常处理器生成未知错误
ResultInfo resultInfo = ResultUtil.createFail(Config.MESSAGE,
900, null);
throw new ExceptionResultInfo(resultInfo);
}
}
// 返回一个登陆页面
return "login" ;
|