簡體   English   中英

Spring Boot與Spring安全性登錄表單

[英]Spring boot with spring security login form

我有一個簡單的Web應用程序,使用過Spring Boot + Hibernate + thymeleaf + Spring Security。 我已經制作了注冊系統,它可以正常工作。 我試圖做登錄系統,但是我不知道我是否了解spring安全思想。 這是我的securityConfig:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM