목록프로그래밍 (409)
쌓고 쌓다
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/C2Qtl/btsDEihkPxD/FhcqpAimaGyFb7lGPzS6vK/img.png)
Authentication(인증) : 누구인지 확인하는 절차로 실제 사용자가 맞는지 로그인하는 과정이라고 생각하면 된다. Authorization(권한부여) : 인증된 사용자에 대해 특정 리소스에 접근이 가능한 권한을 부여하거나 확인하는 과정이다. build.gradle implementation group: 'com.auth0', name: 'java-jwt', version: '4.4.0' JwtProperties.java package com.example.spotserver.config.jwt; import org.springframework.beans.factory.annotation.Value; public class JwtProperties { @Value("${jwt.secrectKey}"..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/bIhSN6/btsDCN2Hxjd/omjUaomGnV4cwTBW1YjEg0/img.png)
시큐리티 세션 = Authentication ( UserDetails ) 구조이다. 우리가 저장할 정보는 UserDeatils 객체여야하며 그것은 Authentication으로 감싸져있다. 이것을 세션으로 사용한다. JWT를 사용하면 세션을 만들 이유는 없지만. 권한처리 때문에 일단 사용한다고하는데 JWT에 권한도 담겨져 있는것이 아닌가 생각이 들지만 나중에 JWT를 완성해보면 이해가 될것같다. 회원의 정보 TestUser는 다음과 같이 설계했다. TestUser를 UserDetails로 만들자. PrincipalDetails (UserDetails) package com.example.spotserver.config.auth; import com.example.spotserver.securityStud..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/MXIEt/btsDyO12cvI/u17bZRd0FygOnf2OH6pwYk/img.png)
CorsConfig.java package com.example.spotserver.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; @Configuration public class CorsConfig { @Bean public Co..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/cGPTKx/btsDs1184RF/ISkiprLo0ZVF4oDI1T2xJk/img.png)
앞전의 코드에서 다음의 코드가 권한 확인이 안되어서 hasAnyRole("ADMIN", "MANAGER") => hasAnyAuthority 또는 hasAuthority로 변경했다. hasAnyAuthority, hasAuthority가 대소문자를 구분하므로 setRole("user")로 해놓고 권한을 "ADMIN"으로 확인하면 안된다. SecurityConfig @Configuration @EnableWebSecurity @EnableMethodSecurity(securedEnabled = true, prePostEnabled = true) public class SecurityConfig { ... } @Secured, @PreAuthorize, PostAuthorize를 사용하기위해 @EnableM..
SecurityConfig package com.example.spotserver.config; import org.springframework.context.annotation.Bean; 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.c..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/bm48DF/btsDqTppO4D/ySEIZmDJn9K1XeKJO0H6F0/img.png)
BCrypt 는 단방향 암호화로 복호화가 불가능하다! 저장할 회원 테이블은 다음과 같다. CREATE TABLE test_user ( id BIGINT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), password VARCHAR(255), role VARCHAR(255) ); SecurityConfig package com.example.spotserver.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.w..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/0ykHT/btsDp7O5uYN/og1y8v7rAQLUKQPY9BozU1/img.png)
먼저 시큐리티 사용을 위해 Gradle에 추가해주자. build.gradle implementation 'org.springframework.boot:spring-boot-starter-security' 설정파일을 관리하기 위해 다음과 같이 폴더를 구성한다. config 폴더에 SecurityConfig를 만들어준다. SecurityConfig package com.example.spotserver.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotatio..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/71HGu/btsCQocLmB6/vJQ4N7B3Tt5LqRyrWO6wY1/img.png)
만들고 있는 앱에서 서버로 "http://10.0.2.2:8080/inquirys/imagefile/b26295a2-6e46-49c9-9cf9-173fb8ad4a2b.jpg"와 같이 요청이 오면 이미지 파일을 응답으로 보내길 원했다. 그래서 다음과 같은 코드를 작성했다. @GetMapping("/imagefile/{imagefileName}") public ResponseEntity getImagefile(@PathVariable String imagefileName) throws IOException { UrlResource resource = new UrlResource("file:" + imageStore.getFullPath(imagefileName)); return ResponseEntity.o..