Skip to content

Commit b58472b

Browse files
1.0.2: Remove usage of deprecated WebSecurityConfigurerAdapter
1 parent 3f390b0 commit b58472b

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

spring-lemon-commons-web/src/main/java/com/naturalprogrammer/spring/lemon/commonsweb/LemonCommonsWebAutoConfiguration.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.fasterxml.jackson.databind.ObjectMapper;
2020
import com.naturalprogrammer.spring.lemon.commons.LemonCommonsAutoConfiguration;
2121
import com.naturalprogrammer.spring.lemon.commons.LemonProperties;
22-
import com.naturalprogrammer.spring.lemon.commons.exceptions.handlers.BadCredentialsExceptionHandler;
2322
import com.naturalprogrammer.spring.lemon.commonsweb.exceptions.DefaultExceptionHandlerControllerAdvice;
2423
import com.naturalprogrammer.spring.lemon.commonsweb.exceptions.LemonErrorAttributes;
2524
import com.naturalprogrammer.spring.lemon.commonsweb.exceptions.LemonErrorController;
@@ -32,6 +31,7 @@
3231
import org.apache.commons.logging.Log;
3332
import org.apache.commons.logging.LogFactory;
3433
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
34+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3535
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3636
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3737
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
@@ -50,6 +50,8 @@
5050
import org.springframework.data.web.config.EnableSpringDataWebSupport;
5151
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
5252
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
53+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
54+
import org.springframework.security.web.SecurityFilterChain;
5355
import org.springframework.web.cors.CorsConfigurationSource;
5456

5557
import java.io.Serializable;
@@ -107,9 +109,9 @@ public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(
107109
public <T extends Throwable>
108110
DefaultExceptionHandlerControllerAdvice<T> defaultExceptionHandlerControllerAdvice(
109111
ErrorResponseComposer<T> errorResponseComposer) {
110-
111-
log.info("Configuring DefaultExceptionHandlerControllerAdvice");
112-
return new DefaultExceptionHandlerControllerAdvice<T>(errorResponseComposer);
112+
113+
log.info("Configuring DefaultExceptionHandlerControllerAdvice");
114+
return new DefaultExceptionHandlerControllerAdvice<>(errorResponseComposer);
113115
}
114116

115117
/**
@@ -120,8 +122,8 @@ DefaultExceptionHandlerControllerAdvice<T> defaultExceptionHandlerControllerAdvi
120122
public <T extends Throwable>
121123
ErrorAttributes errorAttributes(ErrorResponseComposer<T> errorResponseComposer) {
122124

123-
log.info("Configuring LemonErrorAttributes");
124-
return new LemonErrorAttributes<T>(errorResponseComposer);
125+
log.info("Configuring LemonErrorAttributes");
126+
return new LemonErrorAttributes<>(errorResponseComposer);
125127
}
126128

127129
/**
@@ -148,16 +150,16 @@ public LemonCorsConfigurationSource corsConfigurationSource(LemonProperties prop
148150
log.info("Configuring LemonCorsConfigurationSource");
149151
return new LemonCorsConfigurationSource(properties);
150152
}
151-
153+
152154
/**
153155
* Configures LemonSecurityConfig if missing
154156
*/
155157
@Bean
156-
@ConditionalOnMissingBean(LemonWebSecurityConfig.class)
157-
public LemonWebSecurityConfig lemonSecurityConfig() {
158-
159-
log.info("Configuring LemonWebSecurityConfig");
160-
return new LemonWebSecurityConfig();
158+
@ConditionalOnBean(LemonWebSecurityConfig.class)
159+
public SecurityFilterChain lemonSecurityFilterChain(HttpSecurity http, LemonWebSecurityConfig securityConfig) throws Exception {
160+
161+
log.info("Configuring lemonSecurityFilterChain");
162+
return securityConfig.configure(http).build();
161163
}
162164

163165
/**
@@ -167,9 +169,9 @@ public LemonWebSecurityConfig lemonSecurityConfig() {
167169
@ConditionalOnMissingBean(AuditorAware.class)
168170
public <ID extends Serializable>
169171
AuditorAware<ID> auditorAware() {
170-
171-
log.info("Configuring LemonAuditorAware");
172-
return new LemonWebAuditorAware<ID>();
172+
173+
log.info("Configuring LemonAuditorAware");
174+
return new LemonWebAuditorAware<>();
173175
}
174176

175177
/**

spring-lemon-commons-web/src/main/java/com/naturalprogrammer/spring/lemon/commonsweb/security/LemonWebSecurityConfig.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.commons.logging.LogFactory;
2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
24-
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2524
import org.springframework.security.config.http.SessionCreationPolicy;
2625
import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint;
2726
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@@ -30,28 +29,27 @@
3029
* Security configuration class. Extend it in the
3130
* application, and make a configuration class. Override
3231
* protected methods, if you need any customization.
33-
*
32+
*
3433
* @author Sanjay Patel
3534
*/
36-
public class LemonWebSecurityConfig extends WebSecurityConfigurerAdapter {
37-
35+
public class LemonWebSecurityConfig {
36+
3837
private static final Log log = LogFactory.getLog(LemonWebSecurityConfig.class);
3938

4039
protected BlueTokenService blueTokenService;
41-
40+
4241
@Autowired
4342
public void createLemonWebSecurityConfig(BlueTokenService blueTokenService) {
4443

45-
this.blueTokenService = blueTokenService;
44+
this.blueTokenService = blueTokenService;
4645
log.info("Created");
4746
}
4847

4948
/**
5049
* Security configuration, calling protected methods
5150
*/
52-
@Override
53-
protected void configure(HttpSecurity http) throws Exception {
54-
51+
public HttpSecurity configure(HttpSecurity http) throws Exception {
52+
5553
sessionCreationPolicy(http); // set session creation policy
5654
logout(http); // logout related configuration
5755
exceptionHandling(http); // exception handling
@@ -60,6 +58,7 @@ protected void configure(HttpSecurity http) throws Exception {
6058
cors(http); // CORS configuration
6159
authorizeRequests(http); // authorize requests
6260
otherConfigurations(http); // override this to add more configurations
61+
return http;
6362
}
6463

6564

@@ -135,18 +134,18 @@ protected void cors(HttpSecurity http) throws Exception {
135134
*/
136135
protected void authorizeRequests(HttpSecurity http) throws Exception {
137136
http.authorizeRequests()
138-
.mvcMatchers("/**").permitAll();
137+
.mvcMatchers("/**").permitAll();
139138
}
140-
139+
141140

142141
/**
143142
* Override this to add more http configurations,
144143
* such as more authentication methods.
145-
*
144+
*
146145
* @param http
147146
* @throws Exception
148147
*/
149-
protected void otherConfigurations(HttpSecurity http) throws Exception {
150-
148+
protected void otherConfigurations(HttpSecurity http) {
149+
// Override this method to provide other configurations
151150
}
152151
}

spring-lemon-jpa/src/main/java/com/naturalprogrammer/spring/lemon/security/LemonJpaSecurityConfig.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ public void createLemonSecurityConfig(LemonProperties properties, LemonUserDetai
6767
* Security configuration, calling protected methods
6868
*/
6969
@Override
70-
protected void configure(HttpSecurity http) throws Exception {
71-
70+
public HttpSecurity configure(HttpSecurity http) throws Exception {
71+
7272
super.configure(http);
7373
login(http); // authentication
7474
exceptionHandling(http); // exception handling
7575
oauth2Client(http);
76+
return http;
7677
}
7778

7879

@@ -99,11 +100,11 @@ protected void login(HttpSecurity http) throws Exception {
99100
.failureHandler(new SimpleUrlAuthenticationFailureHandler());
100101
}
101102

102-
103+
103104
/**
104105
* Override this to change login URL
105-
*
106-
* @return
106+
*
107+
* @return String
107108
*/
108109
protected String loginPage() {
109110

0 commit comments

Comments
 (0)