10

Spring-Boot アプリケーションの基本認証を構成しました。すべてが Java Config であり、xml はありません。

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // Authenticate username -> admin, password -> admin & set role as "ROLE_USER"
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
            .antMatchers("/login").permitAll()
            // All Requests should be Authenticated
            .anyRequest().authenticated()
            .and()
            // Enable Basic Authentication
            .httpBasic()
            .and()
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/main", true)
                .loginProcessingUrl("/session")
                .usernameParameter("Username").passwordParameter("Password")
            .and()
            .logout().logoutUrl("/logout").permitAll()
            .and().csrf().disable();
    }

}

基本認証と通常の形式のログインの両方に構成されています。Firefox で Rest-Client からの基本認証をテストしたところ、安全な URL "/main" にアクセスできました。しかし、応答ヘッダーでは、Set-Cookie: JSESSIONID=301225C7AE7C74B0892887389996785D;.

基本認証のために Cookie を生成したくありません。Stateless session基本認証には true が必要です。form-login が機能するには Cookie を生成する必要があるため、Cookie を無効にすることはできません。XML構成については知っていcreate-session="stateless"ますが、基本認証がステートレスでフォーム認証がステートフルになるように、Java構成で同じことを行う方法はありますか?

4

2 に答える 2