해당 내용은 spring 프로젝트를 진행하면서 제가 만난 오류 코드이며, 해당 내용에 대해서 원인과 해결방안을 작성해 보았습니다.

 

 

📌Spring 기초 오류

 

8080 포트로 미동작 (APPLICATION FAILED TO START)

  • 원인
    • 이미 사용 중(Web server failed to start. Port 8080 was already in use.)
  • 이유
    • 프로그램 실행 전 미처 확인하지 못함
  • 해결
    1. cmd에 사용중인 port 리스트 출력(netstat -a -o)
    2. 해당 PID 죽이기

Whitelabel Error Page

  • 문제
    • lombok 미연결
  • 해결
    • @controller 위에 @ResponseBody 추가로 해결
    - @RestController = (@Controller + @ResponseBody) 조합

        → RESTful 웹 서비스를 보다 쉽게 개발할 수 있도록


    - controller 역할 : model 객체 만들어 데이터 담고, view 찾는 것

    ↔ @RestController : 객체만 반환, 객체 데이터는 JSON/XML 형식으로 HTTP 응답에 담음

    - 차이 : **@RestController을 표시하면 모든 메소드가 뷰 대신 객체로 작성**  
  - 참고자료
    > [@Controller와 @RestController의 차이점](https://dncjf64.tistory.com/288)

 


DefaultHandlerExceptionResolver

    • MissingServletRequestParameterException
      • 문제
        • Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'name' for method parameter type String is not present]

 


APPLICATION FAILED TO START

  • 문제
    • The bean 'postService', defined in class path resource [hello/hellospring/SpringConfig.class], could not be registered. A bean with that name has already been defined in file [C:\Users\1212g\Downloads\hello-spring\out\production\classes\hello\hellospring\service\PostService.class] and overriding is disabled.
  • 해결
    • 빈 등록을 중복하여 발생하는 문제
    • @Service어노테이션을 삭제(주석)처리 or SpringConfig.class에서 Service등록(@Bean)을 제거

servlet

  • 문제
    • Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
  • 해결
    • private으로 선언만 하는 부분에 final까지 추가

hibernate

  • 문제
    • detached entity passed to persist: hello.hellospring.domain.Post
  • 해결
    • 엔티티 클래스에 @Id를 부여한 필드에 @GeneratedValue를 작성하여 AUTO, SEQUENCE, IDENTITY 전략 등 데이터베이스에게 key 값을 자동 생성하도록 하는 전략을 선택했으나, 추가로 ID 값을 만들어서 DB에 전송한 경우
    • → 해당 추가 ID 넣는 부분 제거

원본 : Spring Boot 오류 해결

'Spring > error' 카테고리의 다른 글

Security 사용하는 페이지 접근 오류  (0) 2023.03.06
UnsupportedClassVersionError  (0) 2022.07.05

layout: post
title: Security 페이지 접근 오류
subtitle: 회원가입 기능 개발 중 발생한 오류 정리
categories: spring
tags: [flavor-spot-finder, security, spring, study]


issue

📌 Security 주의사항

이전에 자주 사용되던 'WebSecurityConfigurerAdapter'는 Spring Security 5.7.0-M2부터 더이상 사용되지 않습니다.

대신 Spring Security는 'SecurityFilterChain'을 사용하기를 권장하고 있습니다.

SecurityFilterChain

WebSecurityConfigurerAdapter의 'webSecurityCustomizer()'와의 차이점

: SecurityFilterChain을 반환하고 빈으로 등록함으로써 컴포넌트 기반의 보안 설정이 가능합니다.

1. 문제 발생 원인

build.gradle에서 dependencies로 "org.springframework.boot:spring-boot-starter-security"를 사용했을 때

Security에 의해 자동으로 localhost:8080/login으로 페이지가 넘어가서 먼저 username과 password를 통해 권한 확인을 받아야 하는 경우

위 구조에서 id = user & password = (서버에 출력되는 password 값)을 사용해도 "권한이 거부되었다"는 메시지가 뜨는 경우

권한 거부 메시지

2. 문제 해결 방법

    public class SecurityConfig {
      @Bean
      public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
          return httpSecurity
                  .httpBasic().disable()
                  .csrf().disable()
                  .cors().and()
                  .authorizeRequests()
                  .antMatchers("/api/**").permitAll()
                  .antMatchers("/api/users/new-user", "/api/users").permitAll()
                  .and()
                  .sessionManagement()
                  .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                  .and()
                  .build();
      }
  }

'Spring > error' 카테고리의 다른 글

Spring Boot 기초 오류  (0) 2023.03.06
UnsupportedClassVersionError  (0) 2022.07.05

오류 메시지

Exception in thread "main" java.lang.UnsupportedClassVersionError
: hello/hellospring/HelloSpringApplication has been compiled by a more recent version of the Java Runtime 
(class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

클래스 파일은 55.0.버전으로 컴파일 되었지만, 현재 컴파일러 52.0 버전이라서 로딩이 불가능

 

해결책

 

[intellj]

Project SDK 버전 변경 : Project Settings - Modules SDK => 11

Java Compiler 버전 변경 : Preferences - Java Compiler => 11

 

[환경 변수]

환경 변수 편집 : 해당 버전의 bin이 존재하는 폴더까지의 경로 추가

ex) C:\Users\~\.jdks\corretto-11.0.15

시스템 변수 편집

: JAVA_HOME -  C:\Users\~\.jdks\corretto-11.0.15

 

[JAVA]

cmd : java -version 확인으로 사용을 원하는 버전의 파일이 맞는지 확인

-> java8이 사용되고 있어서 jdk-18을 다운 받아서 로컬 컴퓨터에서 동작하는 버전의 변경을 진행

'Spring > error' 카테고리의 다른 글

Spring Boot 기초 오류  (0) 2023.03.06
Security 사용하는 페이지 접근 오류  (0) 2023.03.06

+ Recent posts