실적 프로젝트 - 인증 프로세스 Form 인증 구현

slee2·2022년 4월 8일
0

구성

  • 의존성 설정, 환경설정, UI 화면 구성, 기본 CRUD 구성

  • 스프링 시큐리티 보안 기능을 점진적으로 구현 및 완성

  • Springboot, Spring MVC, Spring Data JPA

  • DB-Postgresql Server

이번 내용은 뭔가
프로젝트를 처음부터 만들지 않고,

다 만들어진 프로젝트에 설명을 더하는 방식이다.

그래서 나는 Security의 기능을 알기위한 목적이므로,
html을 만들지 않고 RestController를 이용해 간단하게 프로젝트를 만들었다.

SecurityConfig

package io.security.security.configs;

import org.springframework.context.annotation.Bean;
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;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.security.cert.Extension;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        String password = passwordEncoder().encode("1111");

        auth.inMemoryAuthentication().withUser("user").password(password).roles("USER");
        auth.inMemoryAuthentication().withUser("manager").password(password).roles("MANAGER");
        auth.inMemoryAuthentication().withUser("admin").password(password).roles("ADMIN");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/mypage").hasRole("USER")
                .antMatchers("/message").hasRole("MANAGER")
                .antMatchers("/config").hasRole("ADMIN")
                .anyRequest().authenticated()
        .and()
                .formLogin()
        ;
    }
}

/의 경우 로그인을 하지 않아도 접근가능하도록 만듬.
각 페이지마다 다른 권한의 사용자만 접근이 가능.

WebIgnore

  • js / css / image 파일 등 보안 필터를 적용할 필요가 없는 리소스를 설정
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}

Form 인증 - PasswordEncoder

  • 비밀번호를 안전하게 암호화 하도록 제공

0개의 댓글