首页 文章

Spring MVC中的全局异常保护不起作用

提问于
浏览
0

我在我的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";
}

显示的日志如下

DEBUG org.springframework.security.web.util.matcher.AntPathRequestMatcher - 检查请求的匹配:'/ adduser';反对'/ admin'DEBUG org.springframework.security.web.util.matcher.AntPathRequestMatcher - 检查请求匹配:'/ adduser';反对'/ user'DEBUG org.springframework.security.web.access.intercept.FilterSecurityInterceptor - 安全对象:FilterInvocation:URL:/ addUser;属性:[hasRole('ROLE_ADMIN')] DEBUG org.springframework.security.web.access.intercept.FilterSecurityInterceptor - 以前经过身份验证:org.springframework.security.authentication.UsernamePasswordAuthenticationToken@bad860a5:Principal:org.springframework.security.core . userdetails.User@586034f:用户名:admin;密码保护];启用:true; AccountNonExpired:true; credentialsNonExpired:true; AccountNonLocked:true;授权机构:ROLE_ADMIN;证书:[保护];认证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@fffdaa08:RemoteIpAddress:127.0.0.1; SessionId:52613DC126F07FDCFA50EC1B2841159F;授权机构:ROLE_ADMIN DEBUG org.springframework.security.access.vote.AffirmativeBased - Voter:org.springframework.security.web.access.expression.WebExpressionVoter@4577357d,返回:1 DEBUG org.springframework.security.web.access.intercept .FilterSecurityInterceptor - 授权成功DEBUG org.springframework.security.web.access.intercept.FilterSecurityInterceptor - RunAsManager没有更改Authentication对象DEBUG org.springframework.security.web.FilterChainProxy - / addUser到达额外过滤器链的末尾;继续使用原始链DEBUG org.springframework.web.servlet.DispatcherServlet - 名为'mvc-dispatcher'的DispatcherServlet处理[/ Ushers / addUser]的GET请求DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 查找path / addUser的处理程序方法EBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - 解析来自handler [null]的异常:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法'GET'DEBUG org .springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - 解析来自handler [null]的异常:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法'GET'DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 解析来自handler [null]的异常:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法'GET' WARN org.springframework.web.servlet.PageNotFound - 不支持请求方法'GET'DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView返回DispatcherServlet,名称为'mvc-dispatcher':假设HandlerAdapter已完成请求处理DEBUG org . springframework.web.servlet.DispatcherServlet - 已成功完成请求DEBUG org.springframework.security.web.access.ExceptionTranslationFilter - 链处理正常DEBUG org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder现已清除,请求处理完成

1 回答

  • 0

    此更新解决了这个问题,

    @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";
    }
    

相关问题