본문 바로가기

TIL

2022.07.30.TIL

인증: 로그인

인가: 권한 부여\

 

HTTP는 사용자를 구별하지 못함

 

Spring Security 그래들: 

// 스프링 시큐리티
implementation 'org.springframework.boot:spring-boot-starter-security'

securityConfig 예제코드:

package com.sparta.springcore.security;

import org.springframework.context.annotation.Configuration;
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 // 스프링 Security 지원을 가능하게 함
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
// 어떤 요청이든 '인증'
                .anyRequest().authenticated()//인증
                .and()
                    // 로그인 기능 허용
                    .formLogin()//로그인 기능 사용
                    .defaultSuccessUrl("/")//성공하면 "/"으로 감
                    .permitAll()//관련기능 허용
                .and()
                    // 로그아웃 기능 허용
                    .logout()//로그아웃
                    .permitAll();//관련기능 허용
    }
}

spring web security 디폴트 로그인값:

  • ID: user
  • PW: spirng 로그 확인
  • /logout 하면 로그아웃도 가능

타임리프 모듈 추가

// Thymeleaf (뷰 템플릿 엔진)
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

 

스프링 시큐리티가 모든 요청을 막기 때문에 style.css도 막아서 스타일 적용이 안됨->예외시켜야함

예외시킬거 많음.. 그냥 공식처럼 외울 것

@Configuration
@EnableWebSecurity // 스프링 Security 지원을 가능하게 함
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) {
// h2-console 사용에 대한 허용 (CSRF, FrameOptions 무시)
        web
                .ignoring()
                .antMatchers("/h2-console/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
// 회원 관리 처리 API (POST /user/**) 에 대해 CSRF 무시
        http.csrf()
                .ignoringAntMatchers("/user/**");

        http.authorizeRequests()
// image 폴더를 login 없이 허용
                .antMatchers("/images/**").permitAll()
// css 폴더를 login 없이 허용
                .antMatchers("/css/**").permitAll()
// 회원 관리 처리 API 전부를 login 없이 허용
                .antMatchers("/user/**").permitAll()
// 그 외 어떤 요청이든 '인증'
                .anyRequest().authenticated()
                .and()
// 로그인 기능
                    .formLogin()
                    .loginPage("/user/login")
                    .defaultSuccessUrl("/")
                    .failureUrl("/user/login?error")
                    .permitAll()
                .and()
// 로그아웃 기능
                    .logout()
                    .permitAll();
    }
}

 

unique 디폴트는 false

user이름으로는 테이블 생성 불가(예약어)

@Entity(name = "users")로 테이블 이름 따로 지정 가능

법적으로 비밀번호 암호화는 의무

'TIL' 카테고리의 다른 글

2022.08.02.TIL  (0) 2022.08.03
2022.08.01.TIL  (0) 2022.08.01
2022.07.29.TIL  (0) 2022.07.29
2022.07.28.TIL  (0) 2022.07.28
2022.07.27.TIL  (0) 2022.07.27