Skip to content

Instantly share code, notes, and snippets.

@mehmetcemyucel
Last active March 28, 2021 16:28
Show Gist options
  • Save mehmetcemyucel/5401b47b3dce0b935c34ad2b638b0e6e to your computer and use it in GitHub Desktop.
Save mehmetcemyucel/5401b47b3dce0b935c34ad2b638b0e6e to your computer and use it in GitHub Desktop.
spring-native-example
package com.cem.springnativeexample.configuration.security;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Slf4j
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.csrf().disable();
}
@Autowired
public void configureAuthGlobal(AuthenticationManagerBuilder auth) {
try {
auth.inMemoryAuthentication()
.withUser("viewer").password(passwordEncoder().encode("pass")).roles("VIEWER").and()
.withUser("editor").password(passwordEncoder().encode("pass")).roles("EDITOR", "VIEWER");
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment