在我的Spring MVC Web应用程序中为HTTP路径映射的不明确的处理程序方法

时间:2017-04-30 17:54:44

标签: java xml spring hibernate spring-mvc

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

}

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

}

1 个答案:

答案 0 :(得分:1)

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