我的问题是我想创建一个@ExceptionHandler方法来捕获所有未处理的异常.一旦捕获,我想重定向到当前页面而不是指定一个单独的页面只是为了显示错误.
基本上我如何获取somemethod返回的someview的值,并在下面的方法unhandledExceptionHandler中动态设置它.
@ExceptionHandler(Exception.class)
protected ModelAndView unhandledExceptionHandler(Exception ex){
System.out.println("unhandle exception here!!!");
ModelAndView mv = new ModelAndView();
mv.setViewName("currentview");
mv.addObject("UNHANDLED_ERROR", "UNHANDLED ERROR. PLEASE CONTACT SUPPORT. "+ex.getMessage());
return mv;
}
@RequestMapping(value = "/somepage", method = RequestMethod.GET)
public String somemethod(HttpSession session) throws Exception {
String abc = null;
abc.length();
return "someview";
}
Run Code Online (Sandbox Code Playgroud)
所以在JSP中,我可以将此错误消息呈现回当前页面.
<c:if test="${not empty UNHANDLED_ERROR}">
<div class="messageError"> ${UNHANDLED_ERROR}</div>
</c:if>
Run Code Online (Sandbox Code Playgroud) 我使用自定义登录表单和自定义获得了spring security 4 UserDetailsService.问题是每次我提交j_spring_security_check它总是会重定向回登录页面.感觉它正在处理j_spring_security_check我容器中的另一个资源.
下面是我的配置.我缺少什么?
应用程序的security.xml
<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/login" access="permitAll" />
<security:intercept-url pattern="/logout" access="permitAll" />
<security:intercept-url pattern="/css/**/*.css" access="permitAll" />
<security:intercept-url pattern="/fonts/*.*" access="permitAll" />
<security:intercept-url pattern="/**" access="isAuthenticated()" />
<security:form-login login-page="/login" default-target-url="/landing" authentication-failure-url="/login?error" authentication-success-handler-ref="loginSuccesHandler" />
<security:logout logout-success-url="/logout" />
<security:csrf disabled="true" />
</security:http>
<security:authentication-manager>
<security:authentication-provider ref="daoAuthenticationProvider" />
</security:authentication-manager>
<bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userDetailsService" />
<property name="passwordEncoder" ref="passwordEncoder" />
</bean>
<bean id="userDetailsService" class="a.b.c.MyAppUserDetailService" />
<bean id="loginSuccesHandler" class="a.b.c.LoginSuccessHandler" />
<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
Run Code Online (Sandbox Code Playgroud)
login.jsp的
<body>
<form action="<c:url …Run Code Online (Sandbox Code Playgroud)