m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

Spring Bootでセキュリティに権限を設定する

認可

ユーザーに特定の条件を付与してアクセス権限みたいなものを設定する
今回は1つの権限で1つのURLへのアクセスを制御する。
実際はロールとかいうもので1ユーザーが複数持てるように管理すべきだが面倒なんで…

やること

ユーザーテーブルに権限のエリアを追加
WebSecurityConfigurerAdapterの派生クラスを修正
ユーザーのエンティティクラスで権限を設定するように対応

テーブル

DROP TABLE if EXISTS user;
create table if not exists user (
   id          int          not null auto_increment,
   username    varchar(255) not null,
   password    varchar(255) not null,
   role        varchar(255) not null,
   primary key (id)
);

※roleを追加

セキュリティ設定

WebSecurityConfig.java
package com.example.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
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;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import com.example.handler.SampleAuthenticationFailureHandler;
import com.example.service.UserServiceImpl;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	
	@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception
    {
		// 認可の設定
        httpSecurity
        	.authorizeRequests()
        	.antMatchers("/css/**", "/img/**", "/js/**", "/login").permitAll()	// /loginは認証なしでアクセス可能
        	.antMatchers("/admin/**").hasRole("ADMIN")	// ADMINユーザーのみアクセス可能
        	.antMatchers("/user/**").hasRole("USER")	// USERユーザーのみアクセス可能
        	.anyRequest()
        	.authenticated();
        
        // ログイン設定
        httpSecurity
        	.formLogin()
        	.loginProcessingUrl("/login")	// 認証処理用
        	.loginPage("/login")	// ログインページ
        	.failureHandler(new SampleAuthenticationFailureHandler())	// 認証失敗時のハンドラクラス
        	.usernameParameter("username")	// ユーザー名のパラメータ
        	.passwordParameter("password")	// パスワードのパラメータ
        	.defaultSuccessUrl("/")	// 認証成功時の遷移先
        	.and();
        
        // ログアウト設定
        httpSecurity
        	.logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout**"))       // ログアウト処理のパス
            .logoutSuccessUrl("/login");	// ログイン完了後のパス 
    }
    
    @Configuration
    protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
        @Autowired
        UserServiceImpl userDetailsService;

        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            // 認証するユーザーを設定する
            auth.userDetailsService(userDetailsService);
        }
    }
	
}

エンティティ

User.java
package com.example.entity;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.data.annotation.Transient;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name = "user")
public class User implements UserDetails {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	@Column(name="username")
	private String username;
	
	@Column(name="password")
	private String password;
	
	@Column(name="role")
	private String role;
	
	public String getRole() {
		return this.role;
	}
	
	public void setRole(String role){
		this.role = role;
	}
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// TODO Auto-generated method stub
		Collection<GrantedAuthority> authorityList = new ArrayList<>();
		authorityList.add(new SimpleGrantedAuthority("ROLE_" + this.role));
		return authorityList;
	}

	@Override
	public String getPassword() {
		// TODO Auto-generated method stub
		return this.password;
	}

	@Override
	public String getUsername() {
		// TODO Auto-generated method stub
		return this.username;
	}

	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}

}

※今回は権限テーブルで複数持たないのでUserDetailsを実装したgetAuthoritiesメソッドでROLEを生成して設定するように対応
※ROLE_XXXのような形で設定しておく必要があります。

動作