Java 控制器通知无法捕获异常
我是SpringMVC的新手,目前需要为web服务提供一个身份验证拦截器。如果身份验证失败,我需要拦截器抛出AccessForbiddenException,如果通过,则返回true。我还创建了AuthenticationExceptionController来捕获异常并返回带有HttpStatus的ResponseEntity。但是,当我遇到内部错误500时,我怀疑这是由于AuthenticationExceptionController无法捕获异常。下面是我的代码。有什么建议可以帮我解决吗 AuthenticationInterceptor.javaJava 控制器通知无法捕获异常,java,spring,spring-mvc,exception-handling,Java,Spring,Spring Mvc,Exception Handling,我是SpringMVC的新手,目前需要为web服务提供一个身份验证拦截器。如果身份验证失败,我需要拦截器抛出AccessForbiddenException,如果通过,则返回true。我还创建了AuthenticationExceptionController来捕获异常并返回带有HttpStatus的ResponseEntity。但是,当我遇到内部错误500时,我怀疑这是由于AuthenticationExceptionController无法捕获异常。下面是我的代码。有什么建议可以帮我解决吗
package path.controller;
public class AuthenticationInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
// handle the authentication check
if(authentication fails) {
throw new AccessForbiddenException("access forbidden");
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
}
package path.controller;
public class AccessForbiddenException extends RunTimeException{
public AccessForbiddenException(String message) {
super(message);
}
}
package path.controller;
@ControllerAdvice
public class AuthenticationExceptionController {
@ExceptionHandler(AccessForbiddenException.class)
public ResponseEntity<?> handleException(AccessForbiddenException e) {
return new ResponseEntity<String>(e.getMessage(), HttpStatus.FORBIDDEN);
}
}
accessBankedenException.java
package path.controller;
public class AuthenticationInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
// handle the authentication check
if(authentication fails) {
throw new AccessForbiddenException("access forbidden");
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
}
package path.controller;
public class AccessForbiddenException extends RunTimeException{
public AccessForbiddenException(String message) {
super(message);
}
}
package path.controller;
@ControllerAdvice
public class AuthenticationExceptionController {
@ExceptionHandler(AccessForbiddenException.class)
public ResponseEntity<?> handleException(AccessForbiddenException e) {
return new ResponseEntity<String>(e.getMessage(), HttpStatus.FORBIDDEN);
}
}
AuthenticationExceptionController.java
package path.controller;
public class AuthenticationInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
// handle the authentication check
if(authentication fails) {
throw new AccessForbiddenException("access forbidden");
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
}
package path.controller;
public class AccessForbiddenException extends RunTimeException{
public AccessForbiddenException(String message) {
super(message);
}
}
package path.controller;
@ControllerAdvice
public class AuthenticationExceptionController {
@ExceptionHandler(AccessForbiddenException.class)
public ResponseEntity<?> handleException(AccessForbiddenException e) {
return new ResponseEntity<String>(e.getMessage(), HttpStatus.FORBIDDEN);
}
}
package path.controller;
@控制器建议
公共类身份验证例外控制器{
@ExceptionHandler(AccessForbidenException.class)
公共响应句柄异常(访问禁止异常e){
返回新的ResponseEntity(例如getMessage(),HttpStatus.FORBIDDEN);
}
}
根上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<bean id="contextApplicationContextProvider" class="path.context.provider.ApplicationContextProvider"></bean>
<context:property-placeholder location="classpath:application.properties" />
<context:component-scan base-package="path.**" />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:property-placeholder location="classpath:application.properties" />
<context:annotation-config />
<annotation-driven />
<view-controller path="" view-name="index.html" />
<resources mapping="**" location="/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/" />
<beans:property name="suffix" value="" />
</beans:bean>
<interceptors>
<interceptor>
<mapping path="/**" />
<exclude-mapping path="/login"/>
<exclude-mapping path="/authenticate"/>
<beans:bean class="path.controller.AuthenticationInterceptor" />
</interceptor>
</interceptors>
<context:component-scan base-package="path" use-default-filters="false">
<context:include-filter expression="path.Controller" type="annotation" />
</context:component-scan>
</beans:beans>
servlet context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<bean id="contextApplicationContextProvider" class="path.context.provider.ApplicationContextProvider"></bean>
<context:property-placeholder location="classpath:application.properties" />
<context:component-scan base-package="path.**" />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:property-placeholder location="classpath:application.properties" />
<context:annotation-config />
<annotation-driven />
<view-controller path="" view-name="index.html" />
<resources mapping="**" location="/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/" />
<beans:property name="suffix" value="" />
</beans:bean>
<interceptors>
<interceptor>
<mapping path="/**" />
<exclude-mapping path="/login"/>
<exclude-mapping path="/authenticate"/>
<beans:bean class="path.controller.AuthenticationInterceptor" />
</interceptor>
</interceptors>
<context:component-scan base-package="path" use-default-filters="false">
<context:include-filter expression="path.Controller" type="annotation" />
</context:component-scan>
</beans:beans>
@ControllerAdvice仅捕获在控制器级别引发的异常 因此,如果在控制器调用之前在身份验证级别抛出异常,那么这是徒劳的
相反,您可以尝试在身份验证之前添加一个筛选器,并捕获筛选器中的异常。1。如果您是新手,不要从SpringXML开始;世界在前进。2.发布500错误。3.张贴stacktrace,其中有一个。4.发布请求。