All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fonimus.ssh.shell.complete.DemoSecurity Maven / Gradle / Ivy

The newest version!
package com.github.fonimus.ssh.shell.complete;

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
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.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * Security configuration
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class DemoSecurity
		extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
				.authorizeRequests()
				.requestMatchers(EndpointRequest.to("info")).permitAll()
				.requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR");
	}

	@Bean("customAuthManager")
	@Override
	public AuthenticationManager authenticationManager() throws Exception {
		return super.authenticationManager();
	}

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

	@Bean
	public UserDetailsService userDetailsService() {
		InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
		PasswordEncoder encoder = passwordEncoder();
		manager.createUser(User.withUsername("user").password(encoder.encode("password")).roles("USER").build());
		manager.createUser(User.withUsername("actuator").password(encoder.encode("password")).roles("ACTUATOR").build
				());
		manager.createUser(User.withUsername("admin").password(encoder.encode("admin")).roles("ADMIN", "ACTUATOR")
				.build());
		return manager;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy