【发布时间】:2017-09-28 07:16:15
【问题描述】:
对我的 appconfig-security.xml 文件进行一些更改后,我收到了这个错误
appconfig-security.xml
`
<http auto-config="true">
<intercept-url pattern="/" access="hasAnyRole('ROLE_ADMIN','ROLE_INSTRUCTOR','ROLE_MEMBER')"/>
<intercept-url pattern="/welcome" access="hasAnyRole('ROLE_ADMIN','ROLE_INSTRUCTOR')"/>
<intercept-url pattern="/profile" access="hasRole('ROLE_MEMBER')"/>
<form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/login?error" username-parameter="username" password-parameter="password"/>
<logout logout-success-url="/login?logout" />
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsServiceImpl">
<password-encoder ref="encoder"></password-encoder>
</authentication-provider>
</authentication-manager>
<beans:bean id="userDetailsServiceImpl" class="com.trygym.solution.service.UserDetailsServiceImpl"></beans:bean>
<beans:bean id="encoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<beans:constructor-arg name="strength" value="11"/>
</beans:bean>
`
以前我在数据库中只有一个角色。然后我尝试进行一些安全检查并为用户分配访问权限。然后我结束了那个错误。我没有对web.xml 做任何更改。
web.xml
`
<display-name>Gym Related Web Application</display-name>
<!-- Location of Java @Configuration classes that configure the components that makeup this application -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appconfig-root.xml</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
`
我正在使用弹簧休眠。请任何人都可以帮助我找到可能是错误的位置。谢谢。
HomeController
package com.trygym.solution.web;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
@Controller
class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
private String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
UserController
package com.trygym.solution.web;
import com.trygym.solution.model.User;
import com.trygym.solution.service.SecurityService;
import com.trygym.solution.service.UserService;
import com.trygym.solution.validator.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private SecurityService securityService;
@Autowired
private UserValidator userValidator;
@RequestMapping(value = "/forget_password", method = RequestMethod.POST)
public String forget_password(Model model) {
//model.addAttribute("userForm", new User());
return "forget_password";
}
@RequestMapping(value = "/registration", method = RequestMethod.GET)
public String registration(Model model) {
model.addAttribute("userForm", new User());
return "registration";
}
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
return "registration";
}
userService.save(userForm);
securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());
return "redirect:/welcome";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model, String error, String logout) {
if (error != null)
model.addAttribute("error", "Your username and password is invalid.");
if (logout != null)
model.addAttribute("message", "You have been logged out successfully.");
return "login";
}
@RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
public String welcome(Model model) {
return "welcome";
}
}
【问题讨论】:
-
您对安全配置所做的更改以及异常的确切消息将在这里有所帮助
-
HTTP 状态 500 - 请求处理失败;嵌套异常是 java.lang.IllegalStateException:为 HTTP 路径“localhost:8080”映射的不明确处理程序方法:{public java.lang.String com.trygym.solution.web.UserController.welcome(org.springframework.ui.Model),私有 java.lang.String com.trygym.solution.web.HomeController.home(java.util.Locale,org.springframework.ui.Model)}。这是我得到的错误,之前我在安全配置文件
<intercept-url pattern="/" access="hasRole('ROLE_USER')"/>和<intercept-url pattern="/welcome" access="hasRole('ROLE_USER') -
好吧,从错误消息看来,您将这两种方法(欢迎和主页)映射到路径。我不太确定安全配置会如何影响这一点
-
这和hibernate无关,而是Spring。似乎 Spring 发现一条路径映射到两个不同的端点,而 Spring 不知道在访问端点时实际调用哪个方法。在这种情况下,这似乎是根路径(
"/") -
就是这样。您将welcome 和home 映射到相同的路径和请求方法。这是不支持的。你真的希望欢迎映射到
/吗?如果是这样,为什么。
标签: java xml spring hibernate spring-mvc