Skip to content

Commit ef7d48d

Browse files
committed
chore: update security configuration
1 parent a4d5a41 commit ef7d48d

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

src/main/java/com/github/throyer/common/springboot/configurations/SpringSecurityConfiguration.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static com.github.throyer.common.springboot.constants.SECURITY.LOGIN_ERROR_URL;
77
import static com.github.throyer.common.springboot.constants.SECURITY.LOGIN_URL;
88
import static com.github.throyer.common.springboot.constants.SECURITY.LOGOUT_URL;
9+
import static com.github.throyer.common.springboot.constants.SECURITY.PASSWORD_ENCODER;
910
import static com.github.throyer.common.springboot.constants.SECURITY.PASSWORD_PARAMETER;
1011
import static com.github.throyer.common.springboot.constants.SECURITY.PUBLIC_API_ROUTES;
1112
import static com.github.throyer.common.springboot.constants.SECURITY.SESSION_COOKIE_NAME;
@@ -25,6 +26,9 @@
2526
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
2728
import org.springframework.core.annotation.Order;
29+
import org.springframework.security.authentication.AuthenticationManager;
30+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
31+
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
2832
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
2933
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3034
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -46,13 +50,29 @@ public class SpringSecurityConfiguration {
4650

4751
@Autowired
4852
public SpringSecurityConfiguration(
49-
SessionService sessionService,
50-
AuthorizationMiddleware filter
53+
SessionService sessionService,
54+
AuthorizationMiddleware filter
5155
) {
5256
this.sessionService = sessionService;
5357
this.filter = filter;
5458
}
5559

60+
@Autowired
61+
protected void globalConfiguration(
62+
AuthenticationManagerBuilder authentication
63+
) throws Exception {
64+
authentication
65+
.userDetailsService(sessionService)
66+
.passwordEncoder(PASSWORD_ENCODER);
67+
}
68+
69+
@Bean
70+
public AuthenticationManager authenticationManager(
71+
AuthenticationConfiguration configuration
72+
) throws Exception {
73+
return configuration.getAuthenticationManager();
74+
}
75+
5676
@Bean
5777
public WebSecurityCustomizer webSecurityCustomizer() {
5878
return (web) -> web.ignoring().antMatchers(STATIC_FILES);
@@ -74,8 +94,7 @@ public SecurityFilterChain api(HttpSecurity http) throws Exception {
7494
.disable()
7595
.exceptionHandling()
7696
.authenticationEntryPoint((request, response, exception) -> forbidden(response))
77-
.and()
78-
.userDetailsService(sessionService)
97+
.and()
7998
.sessionManagement()
8099
.sessionCreationPolicy(STATELESS)
81100
.and()
@@ -100,17 +119,15 @@ public SecurityFilterChain app(HttpSecurity http) throws Exception {
100119
.authenticated()
101120
.and()
102121
.csrf()
103-
.disable()
104-
.userDetailsService(sessionService)
122+
.disable()
105123
.formLogin()
106124
.loginPage(LOGIN_URL)
107125
.failureUrl(LOGIN_ERROR_URL)
108126
.defaultSuccessUrl(HOME_URL)
109127
.usernameParameter(USERNAME_PARAMETER)
110128
.passwordParameter(PASSWORD_PARAMETER)
111129
.and()
112-
.rememberMe()
113-
.userDetailsService(sessionService)
130+
.rememberMe()
114131
.key(TOKEN_SECRET)
115132
.tokenValiditySeconds(DAY_MILLISECONDS)
116133
.and()

src/main/java/com/github/throyer/common/springboot/constants/SECURITY.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ public SECURITY(
2525
SECURITY.REFRESH_TOKEN_EXPIRATION_IN_DAYS = refreshTokenExpirationInDays;
2626
}
2727

28+
public static final String[] STATIC_FILES = {
29+
"/robots.txt",
30+
"/font/**",
31+
"/css/**",
32+
"/webjars/**",
33+
"/js/**",
34+
"/favicon.ico",
35+
"/**.html",
36+
"/documentation/**"
37+
};
38+
2839
public static final PublicRoutes PUBLIC_API_ROUTES = create()
2940
.add(GET, "/api", "/api/documentation/**")
3041
.add(POST, "/api/users", "/api/sessions/**", "/api/recoveries/**", "/api/documentation/**");
@@ -57,16 +68,4 @@ public SECURITY(
5768
public static final String ACCEPTABLE_TOKEN_TYPE = SECURITY_TYPE + " ";
5869
public static final String CAN_T_WRITE_RESPONSE_ERROR = "can't write response error.";
5970
public static final Integer BEARER_WORD_LENGTH = SECURITY_TYPE.length();
60-
61-
public static final String[] STATIC_FILES = {
62-
"/robots.txt",
63-
"/font/**",
64-
"/css/**",
65-
"/webjars/**",
66-
"/webjars/",
67-
"/js/**",
68-
"/favicon.ico",
69-
"/**.html",
70-
"/documentation/**"
71-
};
7271
}

src/main/java/com/github/throyer/common/springboot/domain/user/repository/custom/NativeQueryUserRepositoryImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public Optional<User> findByEmail(String email) {
5555
}
5656

5757
@Override
58+
@SuppressWarnings("unchecked")
5859
public Page<User> findAll(Pageable pageable) {
5960
var query = manager
6061
.createNativeQuery(FIND_ALL_USER_FETCH_ROLES, Tuple.class);

src/main/resources/application.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
server.port=${SERVER_PORT:8080}
66

77
# logger
8-
logging.level.springfox.documentation=off
9-
logging.level.org.springframework.web=trace
8+
logging.level.root=info
109
spring.output.ansi.enabled=always
1110
spring.jpa.properties.hibernate.format_sql=true
1211
spring.jpa.show-sql=${DB_SHOW_SQL:true}

src/main/resources/templates/app/fragments/navbar.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<a
3232
th:href="@{/swagger-ui/index.html?configUrl=/documentation/schemas/swagger-config}"
3333
class="nav-link link-dark px-2"
34+
target="_blank"
3435
>
3536
<i class="fas fa-scroll"></i>
3637
Swagger docs
@@ -40,6 +41,7 @@
4041
<a
4142
href="https://github.com/Throyer/springboot-api-crud#spring-boot-api-crud"
4243
class="nav-link link-dark px-2"
44+
target="_blank"
4345
>
4446
<i class="fab fa-github-alt"></i>
4547
Repository

0 commit comments

Comments
 (0)