gpt4 book ai didi

java - Spring MVC Web 应用程序中映射到 HTTP 路径的处理程序方法不明确

转载 作者:行者123 更新时间:2023-12-02 13:17:31 27 4
gpt4 key购买 nike

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>

`

我正在使用 Spring Hibernate。请任何人帮助我找到可能出现错误的位置。谢谢。

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

}

用户 Controller

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

}

最佳答案

这里的问题是 HomeController.homeUserController.welcome 都映射到 / 并且 Spring 无法确定哪个方法访问端点时调用。从一种方法中删除映射可以解决这一特定问题。

关于java - Spring MVC Web 应用程序中映射到 HTTP 路径的处理程序方法不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709523/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com