Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC中的全局异常防御不起作用_Java_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Java Spring MVC中的全局异常防御不起作用

Java Spring MVC中的全局异常防御不起作用,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我在我的Spring MVC应用程序中添加了下面的全局异常定义,并得到了下面的异常,但我不知道是什么问题,我认为我的Spring安全性中存在问题,但我不确定是否有人可以帮助我 全局异常定义 <!-- global exception mapping --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="

我在我的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:Request方法“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-安全对象:过滤器职业:URL:/addUser;属性:[hasRole'ROLE\u ADMIN'] 调试org.springframework.security.web.access.intercept.FilterSecurityInterceptor-以前经过身份验证的:org.springframework.security.authentication。UsernamePasswordAuthenticationToken@bad860a5:Principal:org.springframework.security.core.userdetails。User@586034f:用户名:admin;密码:[受保护];启用:真;AccountNoExpired:正确;无需证明的凭证:真实;AccountNonLocked:true;授予的权限:角色\管理员;凭据:[受保护];认证:正确;详细信息:org.springframework.security.web.authentication。WebAuthenticationDetails@fffdaa08:RemoteIP地址:127.0.0.1;会话ID:52613DC126F07FDCFA50EC1B2841159F;授予的权限:角色\管理员 调试org.springframework.security.access.vote.AffirmativeBased-投票者:org.springframework.security.web.access.expression。WebExpressionVoter@4577357d,返回时间:1 调试org.springframework.security.web.access.intercept.FilterSecurityInterceptor-授权成功 调试org.springframework.security.web.access.intercept.FilterSecurityInterceptor-RunAsManager未更改身份验证对象 DEBUG org.springframework.security.web.FilterChainProxy-/addUser到达附加筛选器链的末尾;继续使用原始链 DEBUG org.springframework.web.servlet.DispatcherServlet-名为“mvc dispatcher”的DispatcherServlet处理[/Usher/addUser]的GET请求 调试org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping-查找path/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” WARN org.springframework.web.servlet.PageNotFound-不支持请求方法“GET” DEBUG org.springframework.web.servlet.DispatcherServlet-返回给名为“mvc dispatcher”的DispatcherServlet的Null ModelAndView:假设HandlerAdapter完成了请求处理 调试org.springframework.web.servlet.DispatcherServlet-已成功完成请求 调试org.springframework.security.web.access.ExceptionTranslationFilter-链正常处理 调试org.springframework.security.web.context.SecurityContextPersistenceFilter-SecurityContextHolder现在已清除,因为请求处理已完成


此更新解决的问题

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