81,092
社区成员
发帖
与我相关
我的任务
分享
/**
* 判断用户是否登录的切面
* @author HEC
*
*/
@Component
@Aspect
@Order(-1)
public class LoginInterceptor {
@Around("JoinPointExpression.loginCheck()")//切点
public Object loginCheck(ProceedingJoinPoint point) throws LoginException,Throwable{
Object result = null ;
//如果是登录请求,直接放通到Controller中
if("com.whtax.edu.controller.common.LoginController".equals(point.getSignature().getDeclaringTypeName())){
result = point.proceed();
}else{//非登录请求,进行是否登录拦截判断
if( SysContext.getSession().getAttribute("user") == null ){ //判断session中是否有用户
throw new LoginException("user not login");//自定义异常,使用异常机制来跳转到页面
}else{
result = point.proceed();
}
}
return result ;
}
}
<!-- 总错误处理-->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.whtax.edu.exception.LoginException">redirect:/login.jsp</prop>
</props>
</property>
<property name="defaultErrorView">
<value>error.jsp</value>
</property>
<property name="defaultStatusCode">
<value>500</value>
</property>
<property name="warnLogCategory">
<value>WARN</value>
</property>
<property name="order" value="1"></property>
</bean>