Spring MVC 中的全局异常定义不起作用

Posted

技术标签:

【中文标题】Spring MVC 中的全局异常定义不起作用【英文标题】:global exception defenition in Spring MVC does not work 【发布时间】:2015-02-12 00:16:02 【问题描述】:

我在我的 Spring MVC 应用程序中添加了以下全局异常的定义并得到了以下异常但我不知道有什么问题我认为我的 Spring 安全性存在问题但我不确定是否有人可以帮助我?

全局异常定义

<!-- global exception mapping -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
    <map>
        <entry key="DataAccessException" value="error" />
    </map>
</property>
<property name="defaultErrorView" value="error"/>
</bean>
<!-- end global exception mapping -->

当此操作发生任何异常时,我得到一个异常“org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法'GET'”

@RequestMapping(value ="/addUser", method = RequestMethod.POST)
public String addUser(@Valid User user, BindingResult result, Model model, @RequestParam("userRole") String role) throws Exception 
    if(result.hasErrors()) 
        return "register";
    
    String hashPassword = new PasswordHashProcessor().getHashPassword(user.getPassword());
    user.setPassword(hashPassword);
    daoService.addUser(user, role);
    List<UserRole> users = daoService.getNotAdminUsers();
    model.addAttribute("users", users);
    return "users";

显示的日志如下

调试 org.springframework.security.web.util.matcher.AntPathRequestMatcher - 检查请求的匹配:'/adduser';反对 '/admin' 调试 org.springframework.security.web.util.matcher.AntPathRequestMatcher - 检查请求的匹配:'/adduser';反对'/用户' 调试 org.springframework.security.web.access.intercept.FilterSecurityInterceptor - 安全对象:FilterInvocation:URL:/addUser;属性:[hasRole('ROLE_ADMIN')] 调试 org.springframework.security.web.access.intercept.FilterSecurityInterceptor - 先前已通过身份验证:org.springframework.security.authentication.UsernamePasswordAuthenticationToken@bad860a5:主体:org.springframework.security.core.userdetails.User@586034f:用户名:admin;密码保护];启用:真; AccountNonExpired:真;凭据非过期:真; AccountNonLocked:真;授予权限:ROLE_ADMIN;凭证:[受保护];已认证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@fffdaa08:RemoteIpAddress:127.0.0.1;会话ID:52613DC126F07FDCFA50EC1B2841159F;授予权限:ROLE_ADMIN 调试 org.springframework.security.access.vote.AffirmativeBased - 投票者:org.springframework.security.web.access.expression.WebExpressionVoter@4577357d,返回:1 DEBUG org.springframework.security.web.access.intercept.FilterSecurityInterceptor - 授权成功 调试 org.springframework.security.web.access.intercept.FilterSecurityInterceptor - RunAsManager 没有改变 Authentication 对象 调试 org.springframework.security.web.FilterChainProxy - /addUser 到达附加过滤器链的末尾;继续原链 调试 org.springframework.web.servlet.DispatcherServlet - 名称为“mvc-dispatcher”的 DispatcherServlet 处理 [/Ushers/addUser] 的 GET 请求 调试 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 查找路径 /addUser 的处理程序方法 EBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - 解决来自处理程序的异常 [null]:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“GET” 调试 org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - 解决来自处理程序 [null] 的异常:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“GET” 调试 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 从处理程序 [null] 解决异常:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“GET” 警告 org.springframework.web.servlet.PageNotFound - 不支持请求方法“GET” 调试 org.springframework.web.servlet.DispatcherServlet - Null ModelAndView 返回到名为“mvc-dispatcher”的 DispatcherServlet:假设 HandlerAdapter 完成了请求处理 调试 org.springframework.web.servlet.DispatcherServlet - 成功完成请求 DEBUG org.springframework.security.web.access.ExceptionTranslationFilter - 链处理正常 调试 org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder 现在清除,请求处理完成

【问题讨论】:

【参考方案1】:

本次更新解决的问题,

@RequestMapping(value ="/addUser", method = RequestMethod.POST)
public String addUser(@ModelAttribute("user") @Valid User user, BindingResult result) 

    if(result.hasErrors()) 
        return "register";
    
    String hashPassword = new PasswordHashProcessor().getHashPassword(user.getPassword());
    user.setPassword(hashPassword);
    daoService.addUser(user);
    return "redirect:/getUsers";

【讨论】:

以上是关于Spring MVC 中的全局异常定义不起作用的主要内容,如果未能解决你的问题,请参考以下文章

使用 Java Config 的全局 CORS 配置在 Spring MVC 中不起作用

为啥自定的springmvc converter不起作用吗

Spring MVC、Rest 和 Shiro。 @RequiresAuthentication 不起作用

使用中间件的全局异常处理在 ASP.Net Core 应用程序中不起作用

Spring MVC Scala App 仅返回 index.html 页面其余路由不起作用

Spring Mvc拦截器不起作用