Java Spring引导和Spring安全登录表单
我有一个简单的web应用程序,我使用了SpringBoot+hibernate+thymeleaf+SpringSecurity。我制作了注册系统,它工作正常。我尝试过登录系统,但我不知道我是否理解spring的安全思想。 这是我的安全配置:Java Spring引导和Spring安全登录表单,java,spring,spring-mvc,spring-boot,spring-security,Java,Spring,Spring Mvc,Spring Boot,Spring Security,我有一个简单的web应用程序,我使用了SpringBoot+hibernate+thymeleaf+SpringSecurity。我制作了注册系统,它工作正常。我尝试过登录系统,但我不知道我是否理解spring的安全思想。 这是我的安全配置: @Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override p
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/register","/","/home").permitAll().and()
.formLogin()
.loginPage("/login").failureUrl("/login").defaultSuccessUrl("/home")
.usernameParameter("nickname")
.passwordParameter("password").permitAll();
}
}
这是我的登录表单:
<form autocomplete="off" action="#" th:action="@{/login}"
th:object="${user}" method="post" class="m-t" role="form"
data-toggle="validator">
<div class="form-group input-group has-feedback">
<span class="input-group-addon">
<span class="glyphicon glyphicon-user"></span>
</span>
<input name="nickname" type="text" th:field="*{nickname}"
placeholder="Nickname" class="form-control" required />
<span class="glyphicon form-control-feedback"
aria-hidden="true"></span>
</div>
<div class="form-group input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-lock"></span>
</span>
<input name="password" type="password" id="password"
placeholder="Password" class="form-control" required />
<span class="glyphicon"
aria-hidden="true"></span>
</div>
<button type="submit"
class="btn btn-primary block full-width m-b">Zaloguj
</button>
</form>
扎洛古伊
我不确定这是否足以让登录系统正常工作?我看不到您必须提供的任何身份验证提供商 如果您使用的是数据库,则必须使用以下各项:
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery(
"select username, password, enabled from user where username=?").passwordEncoder(encoder)
.authoritiesByUsernameQuery(
"select user_username, role from user_roles where user_username=?");
}
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("ROLE_USER");
}
如果您不使用数据库,或者希望将其用于测试目的,则可以使用以下方法:
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery(
"select username, password, enabled from user where username=?").passwordEncoder(encoder)
.authoritiesByUsernameQuery(
"select user_username, role from user_roles where user_username=?");
}
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("ROLE_USER");
}
只有一个配置我没有看到,您需要配置身份验证提供程序,例如,
inMemoryAuthentication
或jdbcauthentication
您可以在Spring官方网站上看到一个非常好的示例,我建议使用标准的username
和password
作为实际表单字段名。这将使密码管理器和其他帮助人员更有可能理解您的表单。