我想用Spring安全性實現一個簡單的登錄頁面。無論我做什麼,在提交表單輸入時總是會收到錯誤Error 405 Request method 'POST' not supported
。
相關文件:
SecurityConfig.java:錯誤405請求方法'POST'不支持Spring Security
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception
{
auth.inMemoryAuthentication().withUser("admin").password("abc123").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.antMatchers("/", "/thank-you", "/faq", "/legal", "/policy").permitAll()
.antMatchers("/admin/**").access("hasRole('ADMIN')")
.and().formLogin().loginPage("/login")
.usernameParameter("ssoId").passwordParameter("password")
.and().csrf()
.and().exceptionHandling().accessDeniedPage("/");
}
}
SecurityWebApplicationInitializer.java:我控制器
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer
{
}
部分:
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView loginPage()
{
return new ModelAndView("WEB-INF/views/login");
}
的pom.xml:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
的login.jsp:
<form action="/login" method="post">
<div>
<label for="j_username">Username</label> <br>
<input type="text" class="form-control" id="j_username" name="ssoId" required />
</div>
<div>
<label for="j_password">Password</label> <br>
<input type="password" class="form-control" id="j_password" name="password" required />
</div>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<div class="form-actions">
<input type="submit" value="Log in" />
</div>
我認爲這個問題是一個事實,即我的$(_ csrf.parameterName}和$ {_ csrf.token}檢查時,他們都是空的。如果您需要任何其他信息,我會很樂意提供。
感謝您的時間,但這仍然會產生完全相同的錯誤:/ – peech
我得到了同樣的問題,但也不知道什麼已經解決:( 鏈接:http://stackoverflow.com/questions/ 34080594/http-405-method-not-allowed – FreezY