Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4주차 과제] 안은소 #21

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
14 changes: 2 additions & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
// Spring Boot Staeter
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'

Expand All @@ -46,19 +47,8 @@ dependencies {
annotationProcessor 'jakarta.persistence:jakarta.persistence-api'
annotationProcessor 'jakarta.annotation:jakarta.annotation-api'

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

// 6장 스프링 시큐리티 응용
implementation 'com.google.guava:guava:30.1.1-jre'
// 정적 리소스 처리
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

// Test
testImplementation 'org.springframework.boot:spring-boot-starter-test'

// 부트 스트랩 추가
implementation 'org.webjars:bootstrap:4.5.3'
}

// 테스트 의존성 라이브러리
Expand All @@ -76,7 +66,7 @@ querydsl {
sourceSets {
main.java.srcDir querydslDir
}
compileQuerydsl {
compileQuerydsl{
options.annotationProcessorPath = configurations.querydsl
}
configurations {
Expand Down
Binary file removed src/.DS_Store
Binary file not shown.
Binary file removed src/main/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
package com.example.urlshortener.common.configuration;

import com.example.urlshortener.common.security.handler.CustomAuthenticationFailureHandler;
import lombok.RequiredArgsConstructor;
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.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfiguration {
private final CustomAuthenticationFailureHandler customAuthenticationFailureHandler;

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable);
Expand All @@ -35,26 +23,12 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN)
)
);
// Chapter 6.1 모든 요청에 HTTPS 적용하기
// http
// .requiresChannel(
// requiresChannel -> requiresChannel.anyRequest().requiresSecure()
// );
http
.authorizeHttpRequests(
authorize -> authorize
// 로그인 보안 관련 엔드포인트는 모두 접근 허용 + 편의를 위해 나머지도 접근 허용
.requestMatchers("/adduser", "/login", "/login-error", "/login-locked").permitAll()
.requestMatchers("/webjars/**", "/static/css/**", "/h2-console/**", "/images/**").permitAll()
// 현재는 모든 요청 허용
.requestMatchers("/dev/ping").permitAll()
.anyRequest().permitAll()
// .anyRequest().authenticated()
);
http
.formLogin(
formLogin -> formLogin
.loginPage("/login")
.defaultSuccessUrl("/index", true).permitAll()
.failureHandler(customAuthenticationFailureHandler)
);

return http.build();
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.urlshortener.domain.url.controller;

import com.example.urlshortener.common.dto.Response;
import com.example.urlshortener.domain.url.dto.ShortenedUrlDto;
import com.example.urlshortener.domain.url.service.UrlService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/short-links/list")
@Tag(name = "🖥🌿 4주차 과제", description = "4주차 과제입니다.")
public class Hw4Controller {
private final UrlService urlService;

@Operation(
summary = "Query Dsl로 Url 조회하기",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
)
@GetMapping("query-dsl")
public Response<List<ShortenedUrlDto>> getShortUrl(@NotBlank @RequestParam("inquiry") String inquiry) {
List<ShortenedUrlDto> shortenedUrls = urlService.getShortUrlsWithQueryDsl(inquiry);
return Response.data(shortenedUrls);
}

@Operation(
summary = "Data JPA로 URL 조회하기",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
)
@GetMapping("jpa")
public Response<List<ShortenedUrlDto>> getShortUrlsWithJpa(@NotBlank @RequestParam("inquiry") String inquiry) {
List<ShortenedUrlDto> shortenedUrls = urlService.getShortUrlsWithJpa(inquiry);
return Response.data(shortenedUrls);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.example.urlshortener.domain.url.controller;

import com.example.urlshortener.common.dto.Response;
import com.example.urlshortener.domain.url.controller.response.ShortUrlResponse;
import com.example.urlshortener.domain.url.dto.ShortenedUrlDto;
import com.example.urlshortener.domain.url.service.UrlService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/short-links")
@Tag(name = "🖥🌿 4주차 추가 과제", description = "id를 사용한 4주차 추가 과제입니다.")
public class Hw4MoreController {
private final UrlService urlService;

@Operation(
summary = "id로 조회하기",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "ID_NOT_FOUND"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
)
@GetMapping("/id/{id}")
public Response<ShortUrlResponse> getShortUrl(@NotNull @PathVariable("id") Long id) {
ShortenedUrlDto shortenedUrl = urlService.getShortUrlById(id);
return Response.data(ShortUrlResponse.from(shortenedUrl));
}

@Operation(
summary = "id로 삭제하기",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "ID_NOT_FOUND"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
)
@GetMapping("id/delete/{id}")
public Response<ShortUrlResponse> deleteShortUrl(@NotNull @PathVariable("id") Long id) {
urlService.deleteShortUrl(id);
return new Response<>(0, "삭제 성공", null);
}

@Operation(
summary = "id에 해당하는 createAt을 현재로 수정하기",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "ID_NOT_FOUND"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
)
@GetMapping("id/update/{id}")
public Response<ShortUrlResponse> updateShortUrl(@NotNull @PathVariable("id") Long id) {
ShortenedUrlDto shortenedUrl = urlService.updateShortUrl(id);
return Response.data(ShortUrlResponse.from(shortenedUrl));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public class UrlController {
private final UrlService urlService;

@Operation(
summary = "URL 단축하기",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
summary = "URL 단축하기",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
)
@PostMapping
public Response<ShortUrlResponse> createShortUrl(@Valid @RequestBody CreateShortUrlRequest request) {
Expand All @@ -36,12 +36,12 @@ public Response<ShortUrlResponse> createShortUrl(@Valid @RequestBody CreateShort
}

@Operation(
summary = "단축 URL 조회하기",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "URL_NOT_FOUND"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
summary = "단축 URL 조회하기",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "URL_NOT_FOUND"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
)
@GetMapping("/{short_id}")
public Response<ShortUrlResponse> getShortUrl(@NotBlank @PathVariable("short_id") String shortId) {
Expand All @@ -50,11 +50,11 @@ public Response<ShortUrlResponse> getShortUrl(@NotBlank @PathVariable("short_id"
}

@Operation(
summary = "Short URL 리디렉션",
responses = {
@ApiResponse(responseCode = "302", description = "FOUND"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
summary = "Short URL 리디렉션",
responses = {
@ApiResponse(responseCode = "302", description = "FOUND"),
@ApiResponse(responseCode = "500", description = "INTERNAL_SERVER_ERROR")
}
)
@GetMapping("/r/{short_id}")
public RedirectView redirectShortUrl(@NotBlank @PathVariable("short_id") String shortId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@AllArgsConstructor
Expand All @@ -19,10 +21,16 @@ public class ShortenedUrlDto {

public static ShortenedUrlDto from(ShortenedUrl shortenedUrl) {
return ShortenedUrlDto.builder()
.id(shortenedUrl.getId())
.shortUrl(shortenedUrl.getShortUrl())
.originUrl(shortenedUrl.getOriginUrl())
.createdAt(shortenedUrl.getCreatedAt())
.build();
.id(shortenedUrl.getId())
.shortUrl(shortenedUrl.getShortUrl())
.originUrl(shortenedUrl.getOriginUrl())
.createdAt(shortenedUrl.getCreatedAt())
.build();
}

public static List<ShortenedUrlDto> from(List<ShortenedUrl> shortenedUrls) {
return shortenedUrls.stream()
.map(ShortenedUrlDto::from)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.urlshortener.domain.url.repository;

import com.example.urlshortener.domain.url.dto.ShortenedUrlDto;
import com.example.urlshortener.domain.url.entity.QShortenedUrl;
import com.example.urlshortener.domain.url.entity.ShortenedUrl;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ShortenedUrlQueryRepository {
private final JPAQueryFactory jpaQueryFactory;

public List<ShortenedUrlDto> getShortUrlsWithQueryDsl(String inquiry) {
QShortenedUrl shortenedUrl = QShortenedUrl.shortenedUrl;
List<ShortenedUrl> shortenedUrls = jpaQueryFactory
.selectFrom(shortenedUrl)
.where(shortenedUrl.originUrl.contains(inquiry))
.fetch();

return ShortenedUrlDto.from(shortenedUrls);
}
}
Loading