Skip to content

Commit

Permalink
- Spring Security application tok중n filter 작업
Browse files Browse the repository at this point in the history
  • Loading branch information
CJW23 committed Jan 14, 2024
1 parent 6905fc7 commit a42d1fc
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@
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.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

@RequiredArgsConstructor
@EnableWebSecurity
@Configuration
public class SecurityConfig {
public class SecurityConfig extends WebSecurityConfiguration {
private final ChattingOAuth2UserService chattingOAuth2UserService;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

http
.authorizeHttpRequests(a ->
a.anyRequest().permitAll())
a.requestMatchers("/test").authenticated()
.anyRequest().permitAll())
.csrf(AbstractHttpConfigurer::disable)
.formLogin(AbstractHttpConfigurer::disable)
.httpBasic(AbstractHttpConfigurer::disable)
//oauth2 체크전에 accessToken 있는지 체크
.addFilterBefore(this.createFilter(), OAuth2LoginAuthenticationFilter.class)
.oauth2Login(config -> {
config
.loginPage("/login")
Expand All @@ -36,6 +42,10 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http.build();
}

private SimpleChattingAuthenticationFilter createFilter() {
return new SimpleChattingAuthenticationFilter("/test");
}

public AuthenticationSuccessHandler successHandler() {
return ((request, response, authentication) -> {
//response.sendRedirect("https://naver.com");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.cjw.chatting.config.security;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

import java.io.IOException;

/**
* 발급한 JwtToken 검증
*/
public class SimpleChattingAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
protected SimpleChattingAuthenticationFilter(String defaultFilterProcessesUrl) {
super(defaultFilterProcessesUrl);
}

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
//헤더에서 Token 받아오겄지
String authorization = request.getHeader("Authorization");
//토큰으로 provider 결정
return this.getAuthenticationManager().authenticate(new SimpleChattingAuthenticationToken(authorization));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.cjw.chatting.config.security;


import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class SimpleChattingAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//받은 토큰 Expire 검증 후 인증 여부 결정
return new SimpleChattingAuthenticationToken(null, null, null);
}

@Override
public boolean supports(Class<?> authentication) {
return (SimpleChattingAuthenticationToken.class.isAssignableFrom(authentication));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.cjw.chatting.config.security;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class SimpleChattingAuthenticationToken extends AbstractAuthenticationToken {
private final Object credential;
private Object principal;

public SimpleChattingAuthenticationToken(String accessToken) {
super(null);
this.credential = accessToken;
this.setAuthenticated(false);
}
public SimpleChattingAuthenticationToken(Collection<? extends GrantedAuthority> authorities, Object principal, String accessToken) {
super(authorities);
this.principal = principal;
this.credential = accessToken;
this.setAuthenticated(true);
}

@Override
public Object getCredentials() {
return null;
}

@Override
public Object getPrincipal() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cjw.chatting.controller.api;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

@GetMapping("/test")
public String test() {
return "test";
}
}

0 comments on commit a42d1fc

Please sign in to comment.