스프링 시큐리티 적용

세피·2021년 11월 10일
0

스프링

목록 보기
2/11
post-thumbnail

스프링 시큐리티 적용

WebSecurityConfig.java

@Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
    	auth
    		.inMemoryAuthentication()
    			.withUser("user")
    				.password("pw")
    				.roles("USER")
    				.and()
    			.withUser("admin")
    				.password("pw")
    				.roles("ADMIN","USER");
    }

에러

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"

저 코드를 활성화 시키자마자 에러라니...이게 대체 뭔가

위에 에러 해결법, {noop}으로 해결

	@Override
	public void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth
			.inMemoryAuthentication()
			.withUser("user")
			.password("{noop}pw")
			.roles("USER")
			.and()
			.withUser("admin")
			.password("{noop}pw")
			.roles("ADMIN", "USER");
	}

그래서 이유는? 진심 모르겠음

서치해본 결과 : 스프링 시큐리티가 업데이트 되면서 뭔가 바뀌었다고 함. 그게 뭔지는 모르메..

이유 찾음

위에 에러 해결법, {noop}으로 해결
여기서, noop는 Spring Security에서 텍스트 그대로 비밀번호로 인식하게 해줍니다. {noop} 또는 비밀번호 암호화 둘중
하나는 해줘야함
이유 : PasswordEncoder 를 설정하지 않아서였다. Spring boot 는 PasswordEncoder 를 설정해 주지않으며,
이부분은 개발자가 직접 등록 해야한다

공식 사이트 : https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-format

https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released

방법 찾은 블로그 : https://mkyong.com/spring-boot/spring-security-there-is-no-passwordencoder-mapped-for-the-id-null/

Resources 인식 문제

CSS/JS/IMG --> 각종 리소스 파일 인식 문제 해결

Spring과 다르게 SpringBoot는 리소스 폴더를 static 안에 넣는다 해서 넣고 실행 시켰는데

이미지에 엑박 뜸

내가 마주한 문제

  • 스프링부트 - 타임리프 면 저 위치에 넣어도 OK , 인식이 되지만
  • 나는 JSP 파일이었음

그래서 해결법

블로그 : https://tragramming.tistory.com/m/95

MvcConfig.java

/*
	 * class : addResourceHandlers 정적 콘텐츠를 제공할 리소스 위치를 하나 이상 추가합니다.
	 * method :ResourceHandlerRegistry를 통해 리소스 위치와 이 리소스와 매칭될 url을 등록합니다. 
	 * method :addResourceLocations를 통해 리소스 위치 등록
	 * */
	@Override 
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		/* '/js/**'로 호출하는 자원은 '/static/js/' 폴더 아래에서 찾는다. */ 
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
		/* '/css/**'로 호출하는 자원은 '/static/css/' 폴더 아래에서 찾는다. */ 
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
		/* '/img/**'로 호출하는 자원은 '/static/img/' 폴더 아래에서 찾는다. */ 
        registry.addResourceHandler("/img/**").addResourceLocations("classpath:/static/img/");
        registry.addResourceHandler("/font/**").addResourceLocations("classpath:/static/font/");
	}

그 다음은? /resources 지워버림

profile
세피의 블로그입니다

0개의 댓글

관련 채용 정보