Skip to content

Commit

Permalink
refactor : Separate the combined configures into JwtProvider and Secu…
Browse files Browse the repository at this point in the history
…reRequest
  • Loading branch information
wwan13 committed Jun 6, 2024
1 parent d48b667 commit 54f59a0
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 408 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@
import io.wwan13.wintersecurity.auth.provider.BearerTokenExtractor;
import io.wwan13.wintersecurity.auth.provider.HttpRequestAccessManager;
import io.wwan13.wintersecurity.jwt.TokenDecoder;
import io.wwan13.wintersecurity.jwt.provider.JwtTokenDecoder;
import io.wwan13.wintersecurity.secretkey.SecretKey;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

public class AuthConfiguration {

@Bean
@ConditionalOnMissingBean(TokenDecoder.class)
public TokenDecoder tokenDecoder(SecretKey secretKey) {
return new JwtTokenDecoder(secretKey);
}

@Bean
public TokenExtractor tokenExtractor() {
return new BearerTokenExtractor();
Expand All @@ -50,4 +61,9 @@ public AbstractInterceptorAuthProcessor authProcessor(
requestAccessManager
);
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@

public class AuthorizedRequestRegistrar {

private final WebSecurityConfigurer webSecurityConfigurer;
private final SecureRequestConfigurer secureRequestConfigurer;

public AuthorizedRequestRegistrar(WebSecurityConfigurer webSecurityConfigurer) {
this.webSecurityConfigurer = webSecurityConfigurer;
public AuthorizedRequestRegistrar(SecureRequestConfigurer secureRequestConfigurer) {
this.secureRequestConfigurer = secureRequestConfigurer;
}

@Bean
public AuthorizedRequest authorizedRequest() {
AuthorizedRequestRegistry registry = AuthorizedRequestRegistry.of();
webSecurityConfigurer.registerAuthPatterns(registry);
secureRequestConfigurer.registerAuthPatterns(registry);
return AuthorizedRequestApplier.apply(registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@

import org.springframework.context.annotation.Import;

import java.lang.annotation.*;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Import({
AuthorizedRequestRegistrar.class,
AuthConfiguration.class,
AuthProcessorRegistrar.class,
JwtPropertiesRegistrar.class,
JwtConfiguration.class,
PasswordEncoderConfiguration.class,
TargetAnnotationsRegistrar.class
SecretKeyRegistrar.class
})
public @interface EnableWebSecurity {
}
public @interface EnableJwtProvider {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.wwan13.wintersecurity.config;

import org.springframework.context.annotation.Import;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Import({
AuthorizedRequestRegistrar.class,
AuthConfiguration.class,
AuthProcessorRegistrar.class,
TargetAnnotationsRegistrar.class,
SecretKeyRegistrar.class
})
public @interface EnableSecureRequest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@

public class JwtPropertiesRegistrar {

private final WebSecurityConfigurer webSecurityConfigurer;
private final JwtProviderConfigurer jwtProviderConfigurer;

public JwtPropertiesRegistrar(WebSecurityConfigurer webSecurityConfigurer) {
this.webSecurityConfigurer = webSecurityConfigurer;
public JwtPropertiesRegistrar(JwtProviderConfigurer jwtProviderConfigurer) {
this.jwtProviderConfigurer = jwtProviderConfigurer;
}

@Bean
public JwtProperties jwtProperties() {
JwtPropertiesRegistry registry = new JwtPropertiesRegistry();
webSecurityConfigurer.configureJwt(registry);
jwtProviderConfigurer.configureJwt(registry);
return JwtPropertiesApplier.apply(registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@

package io.wwan13.wintersecurity.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import io.wwan13.wintersecurity.jwt.support.JwtPropertiesRegistry;

public class PasswordEncoderConfiguration {
public interface JwtProviderConfigurer extends SecretKeyConfigurer {

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
void configureJwt(JwtPropertiesRegistry registry);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.wwan13.wintersecurity.config;

import io.wwan13.wintersecurity.secretkey.support.SecretKeyRegistry;

public interface SecretKeyConfigurer {

void configureSecretKey(SecretKeyRegistry registry);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.wwan13.wintersecurity.config;

import io.wwan13.wintersecurity.secretkey.SecretKey;
import io.wwan13.wintersecurity.secretkey.support.SecretKetApplier;
import io.wwan13.wintersecurity.secretkey.support.SecretKeyRegistry;
import org.springframework.context.annotation.Bean;

public class SecretKeyRegistrar {

private final SecretKeyConfigurer configurer;

public SecretKeyRegistrar(SecretKeyConfigurer configurer) {
this.configurer = configurer;
}

@Bean
public SecretKey secretKey() {
SecretKeyRegistry registry = new SecretKeyRegistry();
configurer.configureSecretKey(registry);
return SecretKetApplier.apply(registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
package io.wwan13.wintersecurity.config;

import io.wwan13.wintersecurity.auth.authorizedrequest.support.AuthorizedRequestRegistry;
import io.wwan13.wintersecurity.jwt.support.JwtPropertiesRegistry;
import io.wwan13.wintersecurity.resolve.support.TargetAnnotationsRegistry;

public interface WebSecurityConfigurer {
public interface SecureRequestConfigurer extends SecretKeyConfigurer {

void registerAuthPatterns(AuthorizedRequestRegistry registry);

void configureJwt(JwtPropertiesRegistry registry);

void registerResolveTargets(TargetAnnotationsRegistry registry);
void registerTargetAnnotations(TargetAnnotationsRegistry registry);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@

public class TargetAnnotationsRegistrar {

private final WebSecurityConfigurer webSecurityConfigurer;
private final SecureRequestConfigurer secureRequestConfigurer;

public TargetAnnotationsRegistrar(WebSecurityConfigurer webSecurityConfigurer) {
this.webSecurityConfigurer = webSecurityConfigurer;
public TargetAnnotationsRegistrar(SecureRequestConfigurer secureRequestConfigurer) {
this.secureRequestConfigurer = secureRequestConfigurer;
}

@Bean
public TargetAnnotations targetAnnotations() {
TargetAnnotationsRegistry registry = new TargetAnnotationsRegistry();
webSecurityConfigurer.registerResolveTargets(registry);
secureRequestConfigurer.registerTargetAnnotations(registry);
return TargetAnnotationsApplier.apply(registry);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 54f59a0

Please sign in to comment.