Skip to content

Instantly share code, notes, and snippets.

@Hoid
Created July 13, 2020 03:59
Show Gist options
  • Save Hoid/ba77f13bb8910387896f938aabee2ea6 to your computer and use it in GitHub Desktop.
Save Hoid/ba77f13bb8910387896f938aabee2ea6 to your computer and use it in GitHub Desktop.
Spring Security stuff
# AWS port configuration
server.port=5000
# Spring database driver
spring.datasource.url=jdbc:mariadb://REDACTED
spring.datasource.username=REDACTED
spring.datasource.password=REDACTED
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
import org.springframework.stereotype.Component;
@Component
public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException)
throws IOException, ServletException {
response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\"");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
PrintWriter writer = response.getWriter();
writer.println("HTTP Status 401 - " + authException.getMessage());
super.commence(request, response, authException);
}
@Override
public void afterPropertiesSet() throws Exception {
setRealmName("REDACTED");
super.afterPropertiesSet();
}
}
package com.audire.sonaserver.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.config.http.SessionCreationPolicy;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/securityNone").permitAll()
.anyRequest().authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.httpBasic()
.and()
.csrf().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password("{noop}passcode").roles("ADMIN");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment