-
Notifications
You must be signed in to change notification settings - Fork 9
43 add security compatibility #57
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
Open
Fayeredd
wants to merge
22
commits into
1701-jan09-java:master
Choose a base branch
from
Fayeredd:43-add-security-compatibility
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
01065f8
Add security compatibility, temporary token generator; testing needed.
Fayeredd 10595c1
update for readability and demo token generator
Fayeredd 2809d71
debugging
Fayeredd ff159cb
bugfix
Fayeredd 7df2a9d
security tested, operational
Fayeredd 70d54ad
Add security compatibility, temporary token generator; testing needed.
Fayeredd f5b81d0
update for readability and demo token generator
Fayeredd d6c09b9
debugging
Fayeredd c9770fe
bugfix
Fayeredd cb0606c
security tested, operational
Fayeredd 3d97a42
Merge branch '43-add-security-compatibility' of https://github.com/Fa…
Fayeredd cb4a191
code clean
Fayeredd 5825af0
bugfix
Fayeredd 76d4c6f
code clean
Fayeredd 8569e90
refactored security into core and extra; core to remain with the API,…
Fayeredd 673e6c5
refactored
Fayeredd 4a3ab96
Remove unneeded classes; move login, token generation, token authenti…
Fayeredd dca0587
fix dependency compatibility
Fayeredd 9ef22cb
Fully Functional. All files included under any package named 'extra' …
Fayeredd a938d0d
Merge branch 'master' into 43-add-security-compatibility
Fayeredd 69623dc
reference fix
Fayeredd bba4301
Merge branch '43-add-security-compatibility' of https://github.com/Fa…
Fayeredd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
2 changes: 1 addition & 1 deletion
2
...n/java/com/revature/DataSourceConfig.java → ...com/revature/config/DataSourceConfig.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package com.revature; | ||
| package com.revature.config; | ||
|
|
||
| import javax.activation.DataSource; | ||
|
|
||
|
|
||
2 changes: 1 addition & 1 deletion
2
src/main/java/com/revature/WebConfig.java → ...n/java/com/revature/config/WebConfig.java
This file contains hidden or 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 hidden or 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,86 @@ | ||
| /* | ||
| * To change this license header, choose License Headers in Project Properties. | ||
| * To change this template file, choose Tools | Templates | ||
| * and open the template in the editor. | ||
| */ | ||
| package com.revature.config; | ||
|
|
||
| import com.revature.security.JwtAuthenticationEntryPoint; | ||
| import com.revature.security.extra.JwtAuthenticationFilter; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
| 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.EnableWebSecurity; | ||
| import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
| import org.springframework.security.config.http.SessionCreationPolicy; | ||
| import org.springframework.security.core.userdetails.UserDetailsService; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
| import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
|
||
| /** | ||
| * | ||
| * @author FayeRedd | ||
| */ | ||
|
|
||
| @SuppressWarnings("SpringJavaAutowiringInspection") | ||
| @Configuration | ||
| @EnableWebSecurity | ||
| @EnableGlobalMethodSecurity(prePostEnabled = true) | ||
| public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ | ||
|
|
||
| @Autowired | ||
| private JwtAuthenticationEntryPoint unauthorizedHandler; | ||
|
|
||
| @Autowired | ||
| private UserDetailsService userDetailsService; | ||
|
|
||
| @Autowired | ||
| public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception{ | ||
| authenticationManagerBuilder | ||
| .userDetailsService(this.userDetailsService) | ||
| .passwordEncoder(passwordEncoder()); | ||
| } | ||
|
|
||
| @Bean | ||
| public PasswordEncoder passwordEncoder() { | ||
| return new BCryptPasswordEncoder(); | ||
| } | ||
|
|
||
| @Bean | ||
| public JwtAuthenticationFilter authenticationFilterBean(){ | ||
| return new JwtAuthenticationFilter(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void configure(HttpSecurity httpSecurity) throws Exception{ | ||
| httpSecurity | ||
| .csrf().disable() | ||
| .exceptionHandling().authenticationEntryPoint(unauthorizedHandler) | ||
| .and() | ||
| .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
| .and() | ||
| .authorizeRequests() | ||
| .antMatchers( | ||
| HttpMethod.GET, | ||
| "/", | ||
| "/*.html", | ||
| "/favicon.ico", | ||
| "/**/*.html", | ||
| "/**/*.css", | ||
| "/**/*.js" | ||
| ).permitAll() | ||
| .antMatchers("/auth/**").permitAll() | ||
| .anyRequest().authenticated(); | ||
|
|
||
| httpSecurity | ||
| .addFilterBefore(authenticationFilterBean(), UsernamePasswordAuthenticationFilter.class); | ||
|
|
||
| httpSecurity.headers().cacheControl(); | ||
|
|
||
| } | ||
| } |
This file contains hidden or 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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/revature/repositories/extra/UserRepository.java
This file contains hidden or 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,22 @@ | ||
| /* | ||
| * To change this license header, choose License Headers in Project Properties. | ||
| * To change this template file, choose Tools | Templates | ||
| * and open the template in the editor. | ||
| */ | ||
| package com.revature.repositories.extra; | ||
|
|
||
| import com.revature.security.extra.model.User; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| /** | ||
| * | ||
| * @author FayeRedd | ||
| */ | ||
|
|
||
| @Repository | ||
| public interface UserRepository extends JpaRepository<User, Long> { | ||
|
|
||
| User findByUsername(String username); | ||
|
|
||
| } |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/revature/security/JwtAuthenticationEntryPoint.java
This file contains hidden or 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,29 @@ | ||
| /* | ||
| * To change this license header, choose License Headers in Project Properties. | ||
| * To change this template file, choose Tools | Templates | ||
| * and open the template in the editor. | ||
| */ | ||
| package com.revature.security; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.Serializable; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import org.springframework.security.core.AuthenticationException; | ||
| import org.springframework.security.web.AuthenticationEntryPoint; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * | ||
| * @author FayeRedd | ||
| */ | ||
| @Component | ||
| public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint, Serializable{ | ||
|
|
||
| private static final long serialVersionUID = -8970718410437077606L; | ||
|
|
||
| @Override | ||
| public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { | ||
| response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/revature/security/JwtAuthenticationRequest.java
This file contains hidden or 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,45 @@ | ||
| /* | ||
| * To change this license header, choose License Headers in Project Properties. | ||
| * To change this template file, choose Tools | Templates | ||
| * and open the template in the editor. | ||
| */ | ||
| package com.revature.security; | ||
|
|
||
| import java.io.Serializable; | ||
|
|
||
| /** | ||
| * | ||
| * @author FayeRedd | ||
| */ | ||
| public class JwtAuthenticationRequest implements Serializable { | ||
|
|
||
| private static final long serialVersionUID = -8445943548965154778L; | ||
|
|
||
| private String username; | ||
| private String password; | ||
|
|
||
| public JwtAuthenticationRequest() { | ||
| super(); | ||
| } | ||
|
|
||
| public JwtAuthenticationRequest(String username, String password) { | ||
| this.setUsername(username); | ||
| this.setPassword(password); | ||
| } | ||
|
|
||
| public String getUsername() { | ||
| return this.username; | ||
| } | ||
|
|
||
| public void setUsername(String username) { | ||
| this.username = username; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return this.password; | ||
| } | ||
|
|
||
| public void setPassword(String password) { | ||
| this.password = password; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/revature/security/exceptions/JwtTokenMissingException.java
This file contains hidden or 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,19 @@ | ||
| /* | ||
| * To change this license header, choose License Headers in Project Properties. | ||
| * To change this template file, choose Tools | Templates | ||
| * and open the template in the editor. | ||
| */ | ||
| package com.revature.security.exceptions; | ||
|
|
||
| /** | ||
| * | ||
| * @author FayeRedd | ||
| */ | ||
| public class JwtTokenMissingException extends RuntimeException{ | ||
|
|
||
| public JwtTokenMissingException(){} | ||
|
|
||
| public JwtTokenMissingException(String message){ | ||
| super(message); | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/revature/security/extra/JwtAuthenticationFilter.java
This file contains hidden or 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,59 @@ | ||
| /* | ||
| * To change this license header, choose License Headers in Project Properties. | ||
| * To change this template file, choose Tools | Templates | ||
| * and open the template in the editor. | ||
| */ | ||
| package com.revature.security.extra; | ||
|
|
||
| import com.revature.security.extra.JwtTokenUtil; | ||
| import java.io.IOException; | ||
|
|
||
| import javax.servlet.FilterChain; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
| import org.springframework.security.core.userdetails.UserDetails; | ||
| import org.springframework.security.core.userdetails.UserDetailsService; | ||
| import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
| import org.springframework.web.filter.OncePerRequestFilter; | ||
|
|
||
| /** | ||
| * | ||
| * @author FayeRedd | ||
| */ | ||
| public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
|
|
||
| @Autowired | ||
| private UserDetailsService userDetailsService; | ||
|
|
||
| @Autowired | ||
| private JwtTokenUtil jwtTokenUtil; | ||
|
|
||
| @Value("${jwt.header}") | ||
| private String tokenHeader; | ||
|
|
||
| @Override | ||
| protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { | ||
| String authToken = request.getHeader(this.tokenHeader); | ||
|
|
||
| String username = jwtTokenUtil.getUsernameFromToken(authToken); | ||
|
|
||
| if(username != null && SecurityContextHolder.getContext().getAuthentication() == null){ | ||
| UserDetails userDetails = this.userDetailsService.loadUserByUsername(username); | ||
|
|
||
| if(jwtTokenUtil.validateToken(authToken, userDetails)){ | ||
| UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); | ||
|
|
||
| authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | ||
|
|
||
| SecurityContextHolder.getContext().setAuthentication(authentication); | ||
| } | ||
| } | ||
| chain.doFilter(request, response); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.