-
Notifications
You must be signed in to change notification settings - Fork 51
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 #67 from rishabhrawat05/security
Implemented Jwt and Oauth2.0 Authentication
- Loading branch information
Showing
23 changed files
with
609 additions
and
72 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
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
20 changes: 0 additions & 20 deletions
20
src/main/java/com/libraryman_api/security/LoginController.java
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
src/main/java/com/libraryman_api/security/SignupController.java
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
src/main/java/com/libraryman_api/security/WebConfiguration.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...raryman_api/security/PasswordEncoder.java → ..._api/security/config/PasswordEncoder.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
87 changes: 87 additions & 0 deletions
87
src/main/java/com/libraryman_api/security/config/WebConfiguration.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,87 @@ | ||
package com.libraryman_api.security.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; | ||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
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.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
import org.springframework.web.filter.CorsFilter; | ||
|
||
import com.libraryman_api.security.jwt.JwtAuthenticationFilter; | ||
|
||
import static org.springframework.security.config.Customizer.withDefaults; | ||
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS; | ||
|
||
@Configuration | ||
@EnableWebSecurity(debug = true) // Do not use (debug=true) in a production system! as this contain sensitive information. | ||
@EnableMethodSecurity(prePostEnabled = true) | ||
public class WebConfiguration { | ||
|
||
private JwtAuthenticationFilter jwtFilter; | ||
|
||
public WebConfiguration(JwtAuthenticationFilter jwtFilter) { | ||
this.jwtFilter=jwtFilter; | ||
} | ||
@Bean | ||
public SecurityFilterChain web(HttpSecurity http) throws Exception { | ||
http | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.authorizeHttpRequests((request) -> request | ||
// make sure it is in order to access the proper Url | ||
|
||
.requestMatchers("/api/signup").permitAll() | ||
.requestMatchers("/api/login").permitAll() | ||
.requestMatchers("/api/logout").permitAll() | ||
.anyRequest().authenticated() | ||
) | ||
.logout(logout->logout | ||
.deleteCookies("LibraryManCookie")) | ||
.sessionManagement(session -> session.sessionCreationPolicy(STATELESS)) | ||
.formLogin(withDefaults()); | ||
|
||
http.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class) | ||
.httpBasic(httpBasic -> {}); | ||
|
||
http.oauth2Login(withDefaults()); | ||
return http.build(); | ||
} | ||
|
||
@Bean | ||
public AuthenticationManager authenticationManager(AuthenticationConfiguration builder) throws Exception { | ||
return builder.getAuthenticationManager(); | ||
} | ||
@Bean | ||
public CorsConfigurationSource corsConfigurationSource() { | ||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
CorsConfiguration corsConfiguration = new CorsConfiguration(); | ||
corsConfiguration.setAllowCredentials(true); | ||
corsConfiguration.addAllowedOriginPattern("*"); | ||
corsConfiguration.addAllowedHeader("Authorization"); | ||
corsConfiguration.addAllowedHeader("Content-Type"); | ||
corsConfiguration.addAllowedHeader("Accept"); | ||
corsConfiguration.addAllowedMethod("POST"); | ||
corsConfiguration.addAllowedMethod("PUT"); | ||
corsConfiguration.addAllowedMethod("GET"); | ||
corsConfiguration.addAllowedMethod("DELETE"); | ||
corsConfiguration.addAllowedMethod("OPTIONS"); | ||
corsConfiguration.setMaxAge(3600L); | ||
|
||
source.registerCorsConfiguration("/**", corsConfiguration); | ||
return source; | ||
} | ||
|
||
@Bean | ||
public CorsFilter corsFilter() { | ||
return new CorsFilter(corsConfigurationSource()); | ||
} | ||
|
||
} |
Oops, something went wrong.