Spring Security Baic認証


Spring Security Baic認証啱
この論文では、どのようにspring securityに構築、配置、カスタマイズしたBaic認証を紹介します.読者はspring mvcに基づいて配置テストを行う必要があります.
Spring Security配置
java configを使って、以下のように構成されています.
@Configuration
@EnableWebSecurity
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
 
    @Autowired
    private MyBasicAuthenticationEntryPoint authenticationEntryPoint;
 
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
          .withUser("user1").password("user1Pass")
          .authorities("ROLE_USER");
    }
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
          .antMatchers("/securityNone").permitAll()
          .anyRequest().authenticated()
          .and()
          .httpBasic()
          .authenticationEntryPoint(authenticationEntryPoint);
 
        http.addFilterAfter(new CustomFilter(),
          BasicAuthenticationFilter.class);
    }
}
対応xmlの設定は以下の通りです.


 
    
    
        
        
    
 
    
        
            
                
            
        
    
 

もちろんjava configでの配置を推奨します.
主な構成要素の中で注目すべきのは元素であり、それはアプリケーション全体にとってBaic認証を実現するのに十分である.本文の重点ではないので、メモリ方式を使ってユーザーとパスワードを管理します.
テストspring securityアプリケーション
ここではcurlコマンドを直接使ってテストします.windowsプラットフォームはgit Bashを使ってもいいです.まず、私たちは安全証明書を持っていません.
curl -i http://localhost:8080/spring-security-mvc-basic-auth/homepage.html
取得は401を認証情報として返します.
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=E5A8D3C16B65A0A007CFAACAEEE6916B; Path=/spring-security-mvc-basic-auth/; HttpOnly
WWW-Authenticate: Basic realm="Spring Security Application"
Content-Type: text/html;charset=utf-8
Content-Length: 1061
Date: Wed, 29 May 2018 15:14:08 GMT
ブラウザにアクセスすると、異常をブロックし、簡単なダイアログを使って、上記の内容だけを表示するためには、信用状が必要です.
今私たちは同じ資源を要求しますが、アクセス証明書を提供します.
curl -i --user user1:user1Pass http://localhost:8080/spring-security-mvc-basic-auth/homepage.html
現在、サーバ端の応答状態は200で、Cookieを持っています.
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=301225C7AE7C74B0892887389996785D; Path=/spring-security-mvc-basic-auth/; HttpOnly
Content-Type: text/html;charset=ISO-8859-1
Content-Language: en-US
Content-Length: 90
Date: Wed, 29 May 2018 15:19:38 GMT
ブラウザからのアプリケーションは正常にアクセスできますが、唯一の違いは、ログインページが必要ではなく、すべてのBaic認証をサポートするブラウザはダイアログを使ってユーザー証明書の入力を促すことです.
さらに配置する——入り口点
デフォルトの場合は、spring securityは、401未認証のBaicAuthentication Entication Point類を提供しています.完全なページに戻り、クライアントに応答して、ブラウザの中でhtml方式でエラーを表示しても大丈夫です.
spring securityは新しい需要を柔軟にサポートするために十分な方法を提供しています.

新しいエントリポイントは標準beanと定義されています.
@Component
public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
 
    @Override
    public void commence
      (HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx) 
      throws IOException, ServletException {
        response.addHeader("WWW-Authenticate", "Basic realm="" + getRealmName() + """);
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        PrintWriter writer = response.getWriter();
        writer.println("HTTP Status 401 - " + authEx.getMessage());
    }
 
    @Override
    public void afterPropertiesSet() throws Exception {
        setRealmName("Baeldung");
        super.afterPropertiesSet();
    }
}
今は直接自分でhttp応答を書いて、応答体の内容を完全にコントロールします.
締め括りをつける
本論文では、例えばスプリングsecurityとBaic認証を使用してウェブページのアクセスを保護します.それぞれxml方式とjava config方式のspring securityを紹介して、curlコマンドでアプリケーションにアクセスしてテストします.最後に、私たちはエントリポイントを定義することによって、エラーメッセージのフォーマットを制御します.