Spring Security / MVC / JPA - >请求方法“POST”不受支持

时间:2016-10-16 11:25:29

标签: spring http spring-mvc spring-security spring-data

我使用Spring Security,Spring MVC和JPA从HTML登录时出错。

这是我的login.HTML

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en" xmlns:th="http://www.thymeleaf.org">>
    <title>Spring Framework Guru</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body class="security-app">
<div class="details">
    <h2>Spring Security - App</h2>
</div>

<form action="/login" method="post">

    <div class="lc-block">
        <div>
            <input type="text" class="style-4" name="username"
                   placeholder="User Name" />
        </div>
        <div>
            <input type="password" class="style-4" name="password"
                   placeholder="Password" />
        </div>
        <div>
            <input type="submit" value="Sign In" class="button red small" />
        </div>
        <th:if test="${param.error ne null}">
            <div class="alert-danger">Invalid username and password.</div>
        </th:if>
        <th:if test="${param.logout ne null}">
            <div class="alert-normal">You have been logged out.</div>
        </th:if>
    </div>
    <input type="hidden" name="${_csrf.parameterName}"
           value="${_csrf.token}" />
</form>

</body>
</html>

这是WebSecurity类:

@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = CustomUserDetailsService.class)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(passwordencoder());
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/hello").access("hasRole('ROLE_ADMIN')")
            .anyRequest().permitAll()
            .and()
            .formLogin().loginPage("/login")
            .usernameParameter("username").passwordParameter("password")
            .and()
            .logout().logoutSuccessUrl("/login?logout")
            .and()
            .exceptionHandling().accessDeniedPage("/403")
            .and()
            .csrf();
}

@Bean(name = "passwordEncoder")
public PasswordEncoder passwordencoder() {
    return new BCryptPasswordEncoder();
}
}

UserDetails服务类:

@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {
private final UserRepository userRepository;
private final UserRolesRepository userRolesRepository;

@Autowired
public CustomUserDetailsService(UserRepository userRepository, UserRolesRepository userRolesRepository) {
    this.userRepository = userRepository;
    this.userRolesRepository = userRolesRepository;
}


@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = userRepository.findByUserName(username);
    if (null == user) {
        throw new UsernameNotFoundException("No user present with username: " + username);
    } else {
        List<String> userRoles = userRolesRepository.findRoleByUserName(username);
        return new CustomUserDetails(user, userRoles);
    }
}

}

我总是有405错误:

  

2016-10-16 12:15:30.710 WARN 2932 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound:不支持请求方法'POST'

任何想法为什么不调用“configure(HttpSecurity http)”。我错过了什么吗?

非常感谢

安德烈

1 个答案:

答案 0 :(得分:0)

尝试添加

.formLogin().loginPage("/xxx").permitAll()
                .defaultSuccessUrl("/xxx")
                .failureUrl("/xxx?error")

另外

Spring MVC Applications中没有找到控制器方法和页面的一个典型原因是Spring通过在当前URL的末尾添加新链接(或表单action =“x”)来构建URL的奇怪映射约定。 “请求方法POST不支持”仅表示您的请求指向没有任何接受POST请求的地方=&gt; URL映射失败。在JSP站点上,您应始终指定

之类的URL
<form action="${pageContext.request.contextPath}/login" method="post">

或使用JSTL

<c:url value="/login" var="loginUrl" />
<form action="${loginUrl}" method="post">

然后,您可以确定您的链接是在URL中的应用程序根目录之后设置的。这样可以避免将来出现许多不必要的问题。