-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from duohui12/duohui12
7주차 과제 - 인가(Authorization) 구현하기
- Loading branch information
Showing
17 changed files
with
277 additions
and
15,820 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/codesoom/assignment/config/SecurityJavaConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.codesoom.assignment.config; | ||
|
||
import com.codesoom.assignment.application.AuthenticationService; | ||
import com.codesoom.assignment.filters.AuthenticationErrorFilter; | ||
import com.codesoom.assignment.filters.JwtAuthenticationFilter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.authentication.HttpStatusEntryPoint; | ||
|
||
import javax.servlet.Filter; | ||
|
||
@Configuration | ||
@EnableGlobalMethodSecurity(prePostEnabled = true) | ||
public class SecurityJavaConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
private AuthenticationService authenticationService; | ||
|
||
@Override | ||
public void configure(HttpSecurity http) throws Exception { | ||
Filter authenticationFilter = new JwtAuthenticationFilter(authenticationManager(), authenticationService); | ||
Filter authenticationErrorFilter = new AuthenticationErrorFilter(); | ||
|
||
http | ||
.csrf().disable() | ||
.addFilter(authenticationFilter) | ||
.addFilterBefore(authenticationErrorFilter | ||
,JwtAuthenticationFilter.class) | ||
.sessionManagement() | ||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
.and() | ||
.exceptionHandling() | ||
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/codesoom/assignment/filters/AuthenticationErrorFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.codesoom.assignment.filters; | ||
|
||
import com.codesoom.assignment.errors.InvalidTokenException; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpFilter; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public class AuthenticationErrorFilter extends HttpFilter{ | ||
@Override | ||
public void doFilter( | ||
HttpServletRequest request | ||
, HttpServletResponse response | ||
, FilterChain chain) throws IOException, ServletException { | ||
|
||
try { | ||
chain.doFilter(request, response); | ||
}catch(InvalidTokenException exception){ | ||
response.sendError(HttpStatus.UNAUTHORIZED.value()); | ||
} | ||
|
||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app/src/main/java/com/codesoom/assignment/filters/JwtAuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.codesoom.assignment.filters; | ||
|
||
import com.codesoom.assignment.application.AuthenticationService; | ||
import com.codesoom.assignment.security.UserAuthentication; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class JwtAuthenticationFilter extends BasicAuthenticationFilter { | ||
private final AuthenticationService authenticationService; | ||
|
||
public JwtAuthenticationFilter(AuthenticationManager authenticationManager, AuthenticationService authenticationService) { | ||
super(authenticationManager); | ||
this.authenticationService = authenticationService; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request | ||
, HttpServletResponse response | ||
, FilterChain chain) throws IOException, ServletException { | ||
|
||
String authorization = request.getHeader("Authorization"); | ||
|
||
if (authorization != null) { | ||
String accessToken = authorization.substring("Bearer ".length()); | ||
Long userId = authenticationService.parseToken(accessToken); | ||
List<String> roles = authenticationService.getUserRoles(userId); | ||
Authentication authentication = new UserAuthentication(userId,roles); | ||
|
||
SecurityContext context = SecurityContextHolder.getContext(); | ||
context.setAuthentication(authentication); | ||
} | ||
|
||
chain.doFilter(request,response); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/codesoom/assignment/security/UserAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.codesoom.assignment.security; | ||
|
||
import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class UserAuthentication extends AbstractAuthenticationToken { | ||
|
||
private final Long userId; | ||
|
||
public UserAuthentication(Long userId, List<String> roles) { | ||
super(authorities(roles)); | ||
this.userId = userId; | ||
} | ||
|
||
private static List<GrantedAuthority> authorities(List<String> roles) { | ||
//return roles.stream().map(SimpleGrantedAuthority::new) | ||
// .collect(Collectors.toList()); | ||
|
||
List<GrantedAuthority> authorities = new ArrayList<>(); | ||
authorities.add(new SimpleGrantedAuthority("USER")); | ||
return authorities; | ||
} | ||
|
||
@Override | ||
public Object getCredentials() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object getPrincipal() { | ||
return userId; | ||
} | ||
|
||
@Override | ||
public boolean isAuthenticated() { | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.