Skip to content

Commit f62851a

Browse files
"feature: springdoc openapi swagger changes"
1 parent fbfb5c5 commit f62851a

File tree

8 files changed

+8
-264
lines changed

8 files changed

+8
-264
lines changed

src/main/java/com/sandeepbegudem/customer/payments/service/config/OpenAPIConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package com.sandeepbegudem.customer.payments.service.config;import io.swagger.v3.oas.models.OpenAPI;import io.swagger.v3.oas.models.info.Info;import io.swagger.v3.oas.models.info.Contact;import io.swagger.v3.oas.models.info.License;import io.swagger.v3.oas.models.servers.Server;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class OpenAPIConfig { @Bean public OpenAPI openApiInformation() { Server localServer = new Server() .url("http://localhost:6789") .description("Localhost Server URL"); Contact contact = new Contact() .email("sandeep.begudem@gmail.com") .name("Sandeep Begudem"); Info info = new Info() .contact(contact) .description("Demo Customer Payments Backend Spring Boot Application") .summary("Demo Customer Payments Backend Spring Boot Application") .title("springboot3-jwt-authorization-junit5-mockito") .version("V1.0.0") .license(new License().name("Apache 2.0").url("http://springdoc.org")); return new OpenAPI().info(info).addServersItem(localServer); }}
1+
package com.sandeepbegudem.customer.payments.service.config;import io.swagger.v3.oas.models.OpenAPI;import io.swagger.v3.oas.models.info.Info;import io.swagger.v3.oas.models.info.Contact;import io.swagger.v3.oas.models.info.License;import io.swagger.v3.oas.models.servers.Server;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class OpenAPIConfig { @Bean public OpenAPI openApiInformation() { Server localServer = new Server() .url("http://localhost:6789") .description("Localhost Server URL"); Contact contact = new Contact() .email("sandeep.begudem@gmail.com") .name("Sandeep Begudem"); Info info = new Info() .contact(contact) .description("Demo Customer Payments Backend Spring Boot Application") .summary("Demo Customer Payments Backend Spring Boot Application") .title("springboot3-jwt-authorization-junit5-mockito") .version("V1.0.0") .license(new License().name("Apache 2.0").url("http://springdoc.org")); return new OpenAPI().info(info).addServersItem(localServer); }}

src/main/java/com/sandeepbegudem/customer/payments/service/config/SecurityConfig.java

+1-78
Original file line numberDiff line numberDiff line change
@@ -1,78 +1 @@
1-
2-
package com.sandeepbegudem.customer.payments.service.config;
3-
4-
5-
import com.sandeepbegudem.customer.payments.service.filter.JwtFilter;
6-
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.context.annotation.Bean;
8-
import org.springframework.context.annotation.Configuration;
9-
import org.springframework.security.authentication.AuthenticationManager;
10-
import org.springframework.security.authentication.AuthenticationProvider;
11-
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
12-
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
13-
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
14-
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
15-
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
16-
import org.springframework.security.config.http.SessionCreationPolicy;
17-
import org.springframework.security.core.userdetails.UserDetailsService;
18-
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
19-
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
20-
import org.springframework.security.crypto.password.PasswordEncoder;
21-
import org.springframework.security.web.SecurityFilterChain;
22-
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
23-
24-
@Configuration
25-
@EnableWebSecurity
26-
@EnableMethodSecurity
27-
public class SecurityConfig {
28-
@Autowired
29-
private JwtFilter authFilter;
30-
31-
@Bean
32-
//authentication
33-
public UserDetailsService userDetailsService() {
34-
return new UserInfoDetailsService();
35-
}
36-
37-
@Bean
38-
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
39-
return http.csrf().disable()
40-
.authorizeHttpRequests()
41-
.requestMatchers("/jwt/authenticate").permitAll()
42-
.and()
43-
.authorizeHttpRequests().requestMatchers("/api/v1/customers/**","/products/**")
44-
.authenticated().and()
45-
.sessionManagement()
46-
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
47-
.and()
48-
.authenticationProvider(authenticationProvider())
49-
.addFilterBefore(authFilter, UsernamePasswordAuthenticationFilter.class)
50-
.build();
51-
}
52-
53-
54-
// use this to skip BCryptPasswordEncoder
55-
@Bean
56-
public PasswordEncoder passwordEncoder() {
57-
return NoOpPasswordEncoder.getInstance();
58-
}
59-
60-
// @Bean
61-
// public PasswordEncoder passwordEncoder() {
62-
// return new BCryptPasswordEncoder();
63-
// }
64-
65-
@Bean
66-
public AuthenticationProvider authenticationProvider(){
67-
DaoAuthenticationProvider authenticationProvider=new DaoAuthenticationProvider();
68-
authenticationProvider.setUserDetailsService(userDetailsService());
69-
authenticationProvider.setPasswordEncoder(passwordEncoder());
70-
return authenticationProvider;
71-
}
72-
@Bean
73-
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
74-
return config.getAuthenticationManager();
75-
}
76-
77-
}
78-
1+
package com.sandeepbegudem.customer.payments.service.config;import com.sandeepbegudem.customer.payments.service.filter.JwtFilter;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.authentication.AuthenticationManager;import org.springframework.security.authentication.AuthenticationProvider;import org.springframework.security.authentication.dao.DaoAuthenticationProvider;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.http.SessionCreationPolicy;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.crypto.password.NoOpPasswordEncoder;import org.springframework.security.crypto.password.PasswordEncoder;import org.springframework.security.web.SecurityFilterChain;import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;@Configuration@EnableWebSecurity@EnableMethodSecuritypublic class SecurityConfig { @Autowired private JwtFilter authFilter; @Bean //authentication public UserDetailsService userDetailsService() { return new UserInfoDetailsService(); } private static final String[] AUTH_WHITE_LIST = { "/jwt/authenticate", "/v3/api-docs/**", "/swagger-ui/**", "/v2/api-docs/**", "/swagger-resources/**", "/error" }; @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { return http.csrf().disable() .authorizeHttpRequests() .requestMatchers(AUTH_WHITE_LIST).permitAll() .and() .authorizeHttpRequests().requestMatchers("/api/v1/customers/**","/api/v1/products/**") .authenticated().and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authenticationProvider(authenticationProvider()) .addFilterBefore(authFilter, UsernamePasswordAuthenticationFilter.class) .build(); } // use this to skip BCryptPasswordEncoder @Bean public PasswordEncoder passwordEncoder() { return NoOpPasswordEncoder.getInstance(); }// @Bean// public PasswordEncoder passwordEncoder() {// return new BCryptPasswordEncoder();// } @Bean public AuthenticationProvider authenticationProvider(){ DaoAuthenticationProvider authenticationProvider=new DaoAuthenticationProvider(); authenticationProvider.setUserDetailsService(userDetailsService()); authenticationProvider.setPasswordEncoder(passwordEncoder()); return authenticationProvider; } @Bean public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception { return config.getAuthenticationManager(); }}

src/main/java/com/sandeepbegudem/customer/payments/service/config/UserInfoDetails.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
package com.sandeepbegudem.customer.payments.service.config;import com.sandeepbegudem.customer.payments.service.entity.UserInfo;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.authority.SimpleGrantedAuthority;import org.springframework.security.core.userdetails.UserDetails;import java.util.Arrays;import java.util.Collection;import java.util.List;import java.util.stream.Collectors;public class UserInfoDetails implements UserDetails { private String name; private String password; private List<GrantedAuthority> authorities; public UserInfoDetails(UserInfo userInfo) { name=userInfo.getName(); password=userInfo.getPassword(); authorities= Arrays.stream(userInfo.getRoles().split(",")) .map(SimpleGrantedAuthority::new) .collect(Collectors.toList()); } @Override public Collection<? extends GrantedAuthority> getAuthorities() { return authorities; } @Override public String getPassword() { return password; } @Override public String getUsername() { return name; } @Override public boolean isAccountNonExpired() { return true; } @Override public boolean isAccountNonLocked() { return true; } @Override public boolean isCredentialsNonExpired() { return true; } @Override public boolean isEnabled() { return true; }}
1+
package com.sandeepbegudem.customer.payments.service.config;import com.sandeepbegudem.customer.payments.service.entity.UserInfo;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.authority.SimpleGrantedAuthority;import org.springframework.security.core.userdetails.UserDetails;import java.util.Arrays;import java.util.Collection;import java.util.List;import java.util.stream.Collectors;public class UserInfoDetails implements UserDetails { private String name; private String password; private List<GrantedAuthority> authorities; public UserInfoDetails(UserInfo userInfo) { name=userInfo.getName(); password=userInfo.getPassword(); authorities= Arrays.stream(userInfo.getRoles().split(",")) .map(SimpleGrantedAuthority::new) .collect(Collectors.toList()); } @Override public Collection<? extends GrantedAuthority> getAuthorities() { return authorities; } @Override public String getPassword() { return password; } @Override public String getUsername() { return name; } @Override public boolean isAccountNonExpired() { return true; } @Override public boolean isAccountNonLocked() { return true; } @Override public boolean isCredentialsNonExpired() { return true; } @Override public boolean isEnabled() { return true; }}

src/main/java/com/sandeepbegudem/customer/payments/service/config/UserInfoDetailsService.java

+1-28
Original file line numberDiff line numberDiff line change
@@ -1,28 +1 @@
1-
2-
package com.sandeepbegudem.customer.payments.service.config;
3-
4-
import com.sandeepbegudem.customer.payments.service.repository.UserRepository;
5-
import com.sandeepbegudem.customer.payments.service.repository.UserRepository;
6-
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.security.core.userdetails.UserDetails;
8-
import org.springframework.security.core.userdetails.UserDetailsService;
9-
import org.springframework.security.core.userdetails.UsernameNotFoundException;
10-
import org.springframework.stereotype.Component;
11-
12-
import java.util.Optional;
13-
14-
@Component
15-
public class UserInfoDetailsService implements UserDetailsService {
16-
17-
@Autowired
18-
private UserRepository repository;
19-
20-
@Override
21-
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
22-
Optional<com.sandeepbegudem.customer.payments.service.entity.UserInfo> userInfo = repository.findByName(username);
23-
return userInfo.map(UserInfoDetails::new)
24-
.orElseThrow(() -> new UsernameNotFoundException("user not found " + username));
25-
26-
}
27-
}
28-
1+
package com.sandeepbegudem.customer.payments.service.config;import com.sandeepbegudem.customer.payments.service.repository.UserRepository;import com.sandeepbegudem.customer.payments.service.repository.UserRepository;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.core.userdetails.UsernameNotFoundException;import org.springframework.stereotype.Component;import java.util.Optional;@Componentpublic class UserInfoDetailsService implements UserDetailsService { @Autowired private UserRepository repository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Optional<com.sandeepbegudem.customer.payments.service.entity.UserInfo> userInfo = repository.findByName(username); return userInfo.map(UserInfoDetails::new) .orElseThrow(() -> new UsernameNotFoundException("user not found " + username)); }}

0 commit comments

Comments
 (0)