apply plugin: 'maven-publish' publishing { publications { customLibrary(MavenPublication) { from components.java } } repositories { maven { name = 'sampleRepo' url = layout.buildDirectory.dir("repo") } } }
Wednesday, September 18, 2024
Gradle to Maven Publish
Add to build.gradle
Spring security example 2
package com.example.demo; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.ProviderManager; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.web.SecurityFilterChain; import static org.springframework.security.config.Customizer.withDefaults; @Configuration public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests( auth -> auth.requestMatchers("/employees/**", "/greeting/**", "/hello/**") .permitAll().anyRequest().authenticated()); http.httpBasic(withDefaults()); http.csrf(csrf -> csrf.disable()); return http.build(); } @Bean public AuthenticationManager authenticationManager(UserDetailsService userDetailsService) { var authenticationProvider = new DaoAuthenticationProvider(); authenticationProvider.setUserDetailsService(userDetailsService); return new ProviderManager(authenticationProvider); } @Bean public UserDetailsService userDetailsService() { UserDetails user = User.withUsername("user1") .password("{noop}secret1") .authorities("read") .roles("USER") .build(); UserDetails userOne = User.withUsername("admin1") .password("{noop}secret1") .authorities("read") .roles("ADMIN") .build(); return new InMemoryUserDetailsManager(user, userOne); } }
Spring security example
package com.example.demo; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.ProviderManager; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.web.SecurityFilterChain; import static org.springframework.security.config.Customizer.withDefaults; @Configuration public class SecurityConfig { @Bean public InMemoryUserDetailsManager userDetailsService(PasswordEncoder passwordEncoder) { UserDetails user = User.withUsername("user1") .password(passwordEncoder.encode("secret1")) .roles("USER") .build(); UserDetails admin = User.withUsername("admin1") .password(passwordEncoder.encode("secret1")) .roles("USER", "ADMIN") .build(); return new InMemoryUserDetailsManager(user, admin); } @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http.authorizeHttpRequests(request -> request.anyRequest() .authenticated()) .httpBasic(Customizer.withDefaults()) .build(); } @Bean public PasswordEncoder passwordEncoder() { PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); return encoder; } }
Monday, September 16, 2024
Spring Boot Example
Clone the below repo using the commands on windows git bash
## Get the code # Versions jdk 19 and spring boot 3.3.3 git clone https://github.com/jeethualex/spring.git ## Build project gradle build mvn clean install ## Local run gradle bootRun ## Run the jar file - with gradle java -jar .\build\libs\demo-0.0.1-SNAPSHOT.jar ## Run the jar file - with maven java -jar .\target\demo-0.0.1-SNAPSHOT.jar ###### GREETINGS ###### ## launch urls below # http://localhost:8080/hello curl http://localhost:8080/greeting curl http://localhost:8080/hello # http://localhost:8080/hello?name=G2 curl http://localhost:8080/greeting?name=G2 curl http://localhost:8080/hello?name=G2 ###### EMPLOYEES CRUD ###### ## before running these commands will need a local mysql database server installed with 2 env variables ## will create a spring database with employee table ## Env variables needed to be added on windows are MYSQl_USER and MYSQL_PASS ## Create Employees curl --location 'localhost:8080/employee' \ --header 'Content-Type: application/json' \ --data '{ "name":"jeetu", "department":"IT" }' ## Get all Employees curl --location 'localhost:8080/employees' \ --header 'Content-Type: application/json' ## Get Employee curl --location 'localhost:8080/employee/1' \ --header 'Content-Type: application/json' ## Delete Employee curl --location --request DELETE 'localhost:8080/employee/4' \ --header 'Content-Type: application/json' ## Update Employee curl --location --request PUT 'localhost:8080/employee' \ --header 'Content-Type: application/json' \ --data '{ "id":"1", "name":"jeetu", "department":"HR" }' ###### CUSTOMER JDBC ###### curl http://localhost:8080/customer?name=G2 # Check the logs for db queries
References :
Sunday, July 14, 2024
Friday, July 12, 2024
Saturday, May 30, 2020
Wednesday, October 30, 2019
Saturday, October 19, 2019
Subscribe to:
Posts
(
Atom
)