Skip to content

Commit

Permalink
Format code with standardjs, prettier and google-java-format
Browse files Browse the repository at this point in the history
This commit fixes the style issues introduced in 3ee61d3 according to the output
from standardjs, prettier and google-java-format.

Details: https://deepsource.io/gh/DaNaRim/smart-parking/transform/fc20ca45-885d-4900-9dd1-da9fe4d5ca53/
  • Loading branch information
deepsource-autofix[bot] committed Sep 8, 2022
1 parent 3ee61d3 commit 223adef
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 200 deletions.
@@ -1,7 +1,10 @@
package com.explorers.smartparking.config.security;

import static com.explorers.smartparking.config.spring.MvcConfig.RESOURCES;

import com.explorers.smartparking.user.persistence.model.RoleName;
import com.explorers.smartparking.user.web.failHandler.AuthenticationFailureHandler;
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
Expand All @@ -11,98 +14,94 @@
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.concurrent.TimeUnit;

import static com.explorers.smartparking.config.spring.MvcConfig.RESOURCES;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private final AuthenticationFailureHandler authenticationFailureHandler;
private final UserDetailsServiceImpl userDetailsService;
private final SecurityProperties securityProperties;

public SecurityConfig(AuthenticationFailureHandler authenticationFailureHandler,
UserDetailsServiceImpl userDetailsService,
SecurityProperties securityProperties) {
this.authenticationFailureHandler = authenticationFailureHandler;
this.userDetailsService = userDetailsService;
this.securityProperties = securityProperties;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.mvcMatchers(
RESOURCES.stream()
.map(resource -> "/" + resource + "/**")
.toArray(String[]::new)
).permitAll()
.mvcMatchers(
"/", "/{lang}",
"/login", "/{lang}/login",
"/registration", "/{lang}/registration",
"/registerUser",
"/registrationConfirm",
"/resendRegistrationToken",
"/forgotPassword",
"/sendPassResetToken",
"/resetPasswordPage",
"/updateForgotPassword",
"/updateForgottenPassword",
"/errors/badToken").permitAll()
.mvcMatchers(
"/user/**",
"/parking",
"/park/**").hasRole(RoleName.USER.name())
.mvcMatchers("/guard/**").hasRole(RoleName.GUARD.name())
.mvcMatchers("/parkAdmin/**").hasRole(RoleName.ADMIN.name())
.mvcMatchers("/superAdmin/**").hasRole(RoleName.SUPER_ADMIN.name())
.anyRequest().authenticated()

.and()
.csrf()


// .and()
// .exceptionHandling()
// .accessDeniedPage("/forbidden")

.and()
.formLogin()
.loginPage("/login").permitAll()
.usernameParameter("username")
.passwordParameter("password")
.defaultSuccessUrl("/parking", true)
.failureHandler(authenticationFailureHandler)

.and()
.logout()
.clearAuthentication(true)
.invalidateHttpSession(true)
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.deleteCookies("JSESSIONID", "remember-me")

.and()
.rememberMe()
.key(securityProperties.rememberMeKey())
.rememberMeParameter("remember-me")
.tokenValiditySeconds((int) TimeUnit.DAYS.toSeconds(21))
.userDetailsService(userDetailsService)
.useSecureCookie(true);
}

@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder(11);
}

private final AuthenticationFailureHandler authenticationFailureHandler;
private final UserDetailsServiceImpl userDetailsService;
private final SecurityProperties securityProperties;

public SecurityConfig(
AuthenticationFailureHandler authenticationFailureHandler,
UserDetailsServiceImpl userDetailsService,
SecurityProperties securityProperties) {
this.authenticationFailureHandler = authenticationFailureHandler;
this.userDetailsService = userDetailsService;
this.securityProperties = securityProperties;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.mvcMatchers(
RESOURCES.stream().map(resource -> "/" + resource + "/**").toArray(String[]::new))
.permitAll()
.mvcMatchers(
"/",
"/{lang}",
"/login",
"/{lang}/login",
"/registration",
"/{lang}/registration",
"/registerUser",
"/registrationConfirm",
"/resendRegistrationToken",
"/forgotPassword",
"/sendPassResetToken",
"/resetPasswordPage",
"/updateForgotPassword",
"/updateForgottenPassword",
"/errors/badToken")
.permitAll()
.mvcMatchers("/user/**", "/parking", "/park/**")
.hasRole(RoleName.USER.name())
.mvcMatchers("/guard/**")
.hasRole(RoleName.GUARD.name())
.mvcMatchers("/parkAdmin/**")
.hasRole(RoleName.ADMIN.name())
.mvcMatchers("/superAdmin/**")
.hasRole(RoleName.SUPER_ADMIN.name())
.anyRequest()
.authenticated()
.and()
.csrf()

// .and()
// .exceptionHandling()
// .accessDeniedPage("/forbidden")

.and()
.formLogin()
.loginPage("/login")
.permitAll()
.usernameParameter("username")
.passwordParameter("password")
.defaultSuccessUrl("/parking", true)
.failureHandler(authenticationFailureHandler)
.and()
.logout()
.clearAuthentication(true)
.invalidateHttpSession(true)
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.deleteCookies("JSESSIONID", "remember-me")
.and()
.rememberMe()
.key(securityProperties.rememberMeKey())
.rememberMeParameter("remember-me")
.tokenValiditySeconds((int) TimeUnit.DAYS.toSeconds(21))
.userDetailsService(userDetailsService)
.useSecureCookie(true);
}

@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder(11);
}
}
@@ -1,5 +1,14 @@
package com.explorers.smartparking.config.spring;

import static com.explorers.smartparking.config.spring.MvcConfig.RESOURCES;
import static com.explorers.smartparking.config.spring.MvcConfig.SUPPORTED_LOCALES;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.i18n.LocaleContextHolder;
Expand All @@ -8,64 +17,53 @@
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Stream;

import static com.explorers.smartparking.config.spring.MvcConfig.RESOURCES;
import static com.explorers.smartparking.config.spring.MvcConfig.SUPPORTED_LOCALES;

public class LocaleInterceptor implements HandlerInterceptor {

private final Log logger = LogFactory.getLog(LocaleInterceptor.class);
private final Log logger = LogFactory.getLog(LocaleInterceptor.class);

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String path = request.getRequestURI();
@Override
public boolean preHandle(
HttpServletRequest request, HttpServletResponse response, Object handler) {
String path = request.getRequestURI();

boolean isResourceRequest = Stream.concat(
RESOURCES.stream(),
Stream.of("favicon.ico")
)
.map(resource -> "/" + resource)
.anyMatch(path::startsWith);
boolean isResourceRequest =
Stream.concat(RESOURCES.stream(), Stream.of("favicon.ico"))
.map(resource -> "/" + resource)
.anyMatch(path::startsWith);

if (isResourceRequest
|| !request.getMethod().equals(HttpMethod.GET.toString())
|| path.equals("/login")
|| path.equals("/logout")) {
return true;
}
if (isResourceRequest
|| !request.getMethod().equals(HttpMethod.GET.toString())
|| path.equals("/login")
|| path.equals("/logout")) {
return true;
}

if (path.length() < 3) {
redirect(response, "/" + LocaleContextHolder.getLocale().getLanguage());
return false;
} else {
String lang = path.substring(1, 3);
Locale locale = Locale.forLanguageTag(lang);
if (path.length() < 3) {
redirect(response, "/" + LocaleContextHolder.getLocale().getLanguage());
return false;
} else {
String lang = path.substring(1, 3);
Locale locale = Locale.forLanguageTag(lang);

if (!SUPPORTED_LOCALES.contains(locale)) {
redirect(response, "/%s%s".formatted(LocaleContextHolder.getLocale().getLanguage(), path));
return false;
}
}
Locale requestLocale = Locale.forLanguageTag(path.substring(1, 3));
if (!SUPPORTED_LOCALES.contains(locale)) {
redirect(response, "/%s%s".formatted(LocaleContextHolder.getLocale().getLanguage(), path));
return false;
}
}
Locale requestLocale = Locale.forLanguageTag(path.substring(1, 3));

LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
Objects.requireNonNull(localeResolver).setLocale(request, response, requestLocale);
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
Objects.requireNonNull(localeResolver).setLocale(request, response, requestLocale);

return true;
}
return true;
}

private void redirect(HttpServletResponse response, String path) {
try {
response.sendRedirect(path);
} catch (IOException e) {
logger.error("Error redirecting to " + path, e);
throw new RuntimeException("Error redirecting to " + path, e);
}
private void redirect(HttpServletResponse response, String path) {
try {
response.sendRedirect(path);
} catch (IOException e) {
logger.error("Error redirecting to " + path, e);
throw new RuntimeException("Error redirecting to " + path, e);
}
}
}

0 comments on commit 223adef

Please sign in to comment.