【发布时间】:2015-09-01 20:13:51
【问题描述】:
这是我的 html/thymeleaf 模板。
<form action="/j_spring_security_check" method="POST" class="form-horizontal">
<div class="content">
<h4 class="title">Login Access</h4>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" name="j_username" placeholder="Username" id="username" class="form-control" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" name="j_password" placeholder="Password" id="password" class="form-control" />
</div>
</div>
</div>
</div>
<div class="foot">
<button class="btn btn-danger" data-dismiss="modal" type="submit">Log in</button>
</div>
</form>
我的应用程序.java
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"org.app.demo.repo"})
@EntityScan(basePackages =
{"org.app.demo.domain",
"org.app.friflow.demo.process"
})
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.banner((environment, aClass, printStream) ->
System.out.println(stringBanner()))
.run();
}
}
和安全配置
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private WebServiceAuthenticationEntryPoint unauthorizedHandler;
@Autowired
private TokenProcessingFilter authTokenProcessingFilter;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private UserDetailsService userDetailsService;
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/resources/**", "/login")
.permitAll()
.antMatchers("/greeting")
.hasRole("USER");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
}
}
最后是我的 application.properties 文件
spring.thymeleaf.cache=false
security.basic.enabled=false
每当我提交表单时,我总是会收到此错误。我的配置有错误吗?
【问题讨论】:
-
对于初学者来说,如何配置您使用表单登录的简单事实?在您的配置中没有任何痕迹。其次,在较新的 Spring Security 版本中,默认 URL 是
/login,而不再是/j_spring_security_check。 -
@M.Deinum 如何配置我正在使用表单登录?我不知道那个 j_spring_security_check
-
这就是
formLogin()的用途。如果你不配置什么都不会发生。
标签: java spring spring-mvc spring-security spring-boot