Commit 6f1e2a26 authored by Potharaju Peddi's avatar Potharaju Peddi

for spring security basics

parents
# Default ignored files
/shelf/
/workspace.xml
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<ScalaCodeStyleSettings>
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
</ScalaCodeStyleSettings>
</code_scheme>
</component>
\ No newline at end of file
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="spring-security-latest" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel target="17" />
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="spring-security-latest" options="-parameters" />
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="spring-milestones" />
<option name="name" value="Spring Milestones" />
<option name="url" value="https://artifactory.albertsons.com/artifactory/SafewayPublicGroup/" />
</remote-repository>
<remote-repository>
<option name="id" value="repository.jfrog.deployment.development" />
<option name="name" value="libs-release-local" />
<option name="url" value="https://artifactory.albertsons.com/artifactory/SafewayPublicGroup/" />
</remote-repository>
<remote-repository>
<option name="id" value="archetype" />
<option name="name" value="archetype" />
<option name="url" value="https://artifactory.albertsons.com/artifactory/SafewayPublicGroup/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://artifactory.albertsons.com/artifactory/SafewayPublicGroup/" />
</remote-repository>
<remote-repository>
<option name="id" value="confluent" />
<option name="name" value="confluent" />
<option name="url" value="https://artifactory.albertsons.com/artifactory/SafewayPublicGroup/" />
</remote-repository>
<remote-repository>
<option name="id" value="repository.jfrog.deployment" />
<option name="name" value="Safeway JFrog Repository" />
<option name="url" value="https://artifactory.albertsons.com/artifactory/SafewayPublicGroup/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="libsreleaselocal" />
<option name="name" value="libsreleaselocal" />
<option name="url" value="https://artifactory.albertsons.com/artifactory/SafewayPublicGroup/" />
</remote-repository>
<remote-repository>
<option name="id" value="SafewayPublicGroup" />
<option name="name" value="SafewayPublicGroup" />
<option name="url" value="https://artifactory.albertsons.com/artifactory/SafewayPublicGroup/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK" />
</project>
\ No newline at end of file
X
java:S6813"BRemove this field injection and use constructor injection instead.(êæÄÞ
X
java:S6813"BRemove this field injection and use constructor injection instead.(êæÄÞ
9
java:S2119!"Save and re-use this "Random".(Ž¥ŒÌøÿÿÿÿ
9
java:S2119""Save and re-use this "Random".(«éö™ûÿÿÿÿ
\ No newline at end of file
~ java:S125"<This block of commented-out lines of code should be removed.(Ø8õ2J$3fb65b6f-c2d9-46e3-89ca-420329e78ba2
 java:S125"<This block of commented-out lines of code should be removed.(8ʧ2J$fa0f0a93-381f-471a-81dd-4c7af5120a2a
X java:S125"<This block of commented-out lines of code should be removed.(̉8ϵ2

java:S6204#"zReplace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.(8ϵ2
_
java:S2119!"Save and re-use this "Random".(J$7936c038-e350-446e-9205-b38ef8c52e6c
_
java:S2119""Save and re-use this "Random".(J$c2b281fc-f9f3-4bd4-8526-92f82bdf99ff
 java:S1253"<This block of commented-out lines of code should be removed.(82J$64880dbf-0d78-480e-b0c4-0bbe2ce6a01f
u
java:S1128"SRemove this unused import 'org.springframework.beans.factory.annotation.Autowired'.(8םϵ2
{
java:S1128"YRemove this unused import 'org.springframework.security.crypto.password.PasswordEncoder'.(8؝ϵ2
\ No newline at end of file
exml:S125 "Remove this commented out code.(82J$9224ce2e-beaf-4ded-8881-32d330bb52f0
\ No newline at end of file
7
java:S1220""Move this file to a named package.8£¸Åµ‰2
X java:S125"<This block of commented-out lines of code should be removed.(Ø®…8ª¸Åµ‰2
\ No newline at end of file
7
java:S1220""Move this file to a named package.8½€ò¶‰2
] java:S125"<This block of commented-out lines of code should be removed.(ŒÕÆÿûÿÿÿÿ8¾€ò¶‰2
\ No newline at end of file
7
java:S1220""Move this file to a named package.8µ¹¸¶‰2
X java:S125"<This block of commented-out lines of code should be removed.(Ø®…8¸¹¸¶‰2
\ No newline at end of file
~ java:S125"<This block of commented-out lines of code should be removed.(Ø82J$db69c39c-3c86-438e-b041-fe1fc47cfa1b

java:S6813"BRemove this field injection and use constructor injection instead.(82J$506738e8-13cf-4b38-a113-c4144936f669

java:S1128"URemove this unused import 'org.springframework.security.access.prepost.PreAuthorize'.(82J$90bd755d-450b-413a-ae47-d9d9d4dd59cd
\ No newline at end of file
H
java:S2699
"-Add at least one assertion to this test case.(áùêÌùÿÿÿÿ
\ No newline at end of file
_
java:S6813"BRemove this field injection and use constructor injection instead.(êæÄÞ8‚ëÛŽ‰2
\ No newline at end of file
7
java:S1220""Move this file to a named package.8ì«Àµ‰2
X java:S125"<This block of commented-out lines of code should be removed.(Ø®…8Ù¬Àµ‰2
\ No newline at end of file
y
java:S6437"6Revoke and change this password, as it is compromised.(8Ϲ2J$02779996-3bb2-482e-92a3-754e4b2569a0
~
java:S6437"6Revoke and change this password, as it is compromised.(8Ϲ2J$9b21f26f-cc94-41b5-ab13-15bdba9574e6

java:S1128"_Remove this unused import 'org.springframework.security.authentication.AuthenticationProvider'.(8Ϧ2J$ee1d2ed8-b9a4-4e01-8a40-ffcc67c75d6c

java:S1128"fRemove this unused import 'org.springframework.security.authentication.dao.DaoAuthenticationProvider'.(Â8Ц2J$e442d3f6-bfe7-418a-8416-158790a79845

java:S1128"uRemove this unused import 'org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity'.(82
\ No newline at end of file
9
README.md,8\e\8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d
7
pom.xml,4\4\442292b8a7efeabbe4cc176709b833b1792140ec
n
>src/main/java/com/javatechie/controller/ProductController.java,e\1\e19da54db0eeb6d2570acdbb0d12cb2884b8e342
q
Asrc/main/java/com/javatechie/SpringSecurityLatestApplication.java,b\4\b471fc05c94e329137723193c771587f670895df
h
8src/main/java/com/javatechie/service/ProductService.java,3\3\334274f3c3d9f4d6c6cb1e1daefd0a0ad4ee3c2a
v
Fsrc/test/java/com/javatechie/SpringSecurityLatestApplicationTests.java,d\d\dd4cb18f05730d3c43337ceeb482cfa38e3a3e85
l
<src/main/java/com/security/controller/ProductController.java,b\e\be6944e238152a9b6f8d7fda0d82cdd5e84e2705
f
6src/main/java/com/security/service/ProductService.java,3\7\37aef645278cb9a72a0d67ae86f2f237d99342d9
[
+src/main/java/com/security/dto/Product.java,7\4\74c01e88c7758bc641b292deddd00a9778301bfd
_
/src/main/java/com/security/entity/UserInfo.java,5\b\5ba460316b38b121484d9b74b9a7d8eab5f372a4
m
=src/main/java/com/security/repository/UserInfoRepository.java,e\c\ec6f1a4921102e091b5376eb4c12e21bf3948af8
e
5src/main/java/com/security/config/SecurityConfig.java,e\c\ecc1f75d634e44e274961aec1bbc6bc47d4a9130
j
:src/main/java/com/security/config/UserInfoUserDetails.java,6\2\62f9f22b9dc027ef5e1c6b93742d070fca70b7e1
q
Asrc/main/java/com/security/config/UserInfoUserDetailsService.java,5\4\54e00d613a72644aad1feb4cf80566fed8cf6dbd
o
?src/main/java/com/security/SpringSecurityLatestApplication.java,5\9\596886e7bf3d537b416ccfcffdfce3d5642d8558
\ No newline at end of file
9
README.md,8\e\8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d
7
pom.xml,4\4\442292b8a7efeabbe4cc176709b833b1792140ec
n
>src/main/java/com/javatechie/controller/ProductController.java,e\1\e19da54db0eeb6d2570acdbb0d12cb2884b8e342
q
Asrc/main/java/com/javatechie/SpringSecurityLatestApplication.java,b\4\b471fc05c94e329137723193c771587f670895df
h
8src/main/java/com/javatechie/service/ProductService.java,3\3\334274f3c3d9f4d6c6cb1e1daefd0a0ad4ee3c2a
v
Fsrc/test/java/com/javatechie/SpringSecurityLatestApplicationTests.java,d\d\dd4cb18f05730d3c43337ceeb482cfa38e3a3e85
l
<src/main/java/com/security/controller/ProductController.java,b\e\be6944e238152a9b6f8d7fda0d82cdd5e84e2705
f
6src/main/java/com/security/service/ProductService.java,3\7\37aef645278cb9a72a0d67ae86f2f237d99342d9
[
+src/main/java/com/security/dto/Product.java,7\4\74c01e88c7758bc641b292deddd00a9778301bfd
_
/src/main/java/com/security/entity/UserInfo.java,5\b\5ba460316b38b121484d9b74b9a7d8eab5f372a4
m
=src/main/java/com/security/repository/UserInfoRepository.java,e\c\ec6f1a4921102e091b5376eb4c12e21bf3948af8
e
5src/main/java/com/security/config/SecurityConfig.java,e\c\ecc1f75d634e44e274961aec1bbc6bc47d4a9130
j
:src/main/java/com/security/config/UserInfoUserDetails.java,6\2\62f9f22b9dc027ef5e1c6b93742d070fca70b7e1
q
Asrc/main/java/com/security/config/UserInfoUserDetailsService.java,5\4\54e00d613a72644aad1feb4cf80566fed8cf6dbd
o
?src/main/java/com/security/SpringSecurityLatestApplication.java,5\9\596886e7bf3d537b416ccfcffdfce3d5642d8558
\ No newline at end of file
# spring-boot-security-3.0
Note : If you are using spring boot 3.1.x version then please do the below code change
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth ->
auth.requestMatchers("/product-service/welcome", "/product-service/addNewUser").permitAll()
.requestMatchers("/product-service/**")
.authenticated()
)
.httpBasic(Customizer.withDefaults()).build();
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.security</groupId>
<artifactId>spring-security-latest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-security-latest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-jpa</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.mysql</groupId>-->
<!-- <artifactId>mysql-connector-j</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.security;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringSecurityLatestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSecurityLatestApplication.class, args);
}
}
package com.security.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
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.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
//@EnableMethodSecurity
public class SecurityConfig {
@Bean
//authentication
public UserDetailsService userDetailsService(PasswordEncoder encoder) {
UserDetails admin = User.withUsername("Raju")
.password(encoder.encode("Pa$$Word"))
.roles("ADMIN")
.build();
UserDetails user = User.withUsername("Balu")
.password(encoder.encode("PassV@rd"))
.roles("USER")
.build();
return new InMemoryUserDetailsManager(admin, user);
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http.csrf().disable()
.authorizeHttpRequests()
.requestMatchers("/products/welcome","/products/new").permitAll()
.and()
.authorizeHttpRequests().requestMatchers("/products/**")
.authenticated().and().formLogin().and().build();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
package com.security.controller;
import com.security.dto.Product;
//import com.security.entity.UserInfo;
import com.security.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService service;
@GetMapping("/welcome")
public String welcome() {
return "Welcome this endpoint is not secure";
}
@GetMapping("/all")
//@PreAuthorize("hasAuthority('ROLE_ADMIN')")
public List<Product> getAllTheProducts() {
return service.getProducts();
}
@GetMapping("/{id}")
//@PreAuthorize("hasAuthority('ROLE_USER')")
public Product getProductById(@PathVariable int id) {
return service.getProduct(id);
}
}
package com.security.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Product {
private int productId;
private String name;
private int qty;
private double price;
}
package com.security.service;
import com.security.dto.Product;
//import com.security.entity.UserInfo;
//import com.security.repository.UserInfoRepository;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@Service
public class ProductService {
List<Product> productList = null;
// @Autowired
// private UserInfoRepository repository;
// @Autowired
// private PasswordEncoder passwordEncoder;
@PostConstruct
public void loadProductsFromDB() {
productList = IntStream.rangeClosed(1, 100)
.mapToObj(i -> Product.builder()
.productId(i)
.name("product " + i)
.qty(new Random().nextInt(10))
.price(new Random().nextInt(5000)).build()
).collect(Collectors.toList());
}
public List<Product> getProducts() {
return productList;
}
public Product getProduct(int id) {
return productList.stream()
.filter(product -> product.getProductId() == id)
.findAny()
.orElseThrow(() -> new RuntimeException("product " + id + " not found"));
}
// public String addUser(UserInfo userInfo) {
// userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
// repository.save(userInfo);
// return "user added to system ";
// }
}
spring.security.user.name= Raju
spring.security.user.password= Pa$$word
package com.security;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringSecurityLatestApplicationTests {
@Test
void contextLoads() {
}
}
#spring.security.user.name= Raju
#spring.security.user.password= Pa$$word
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.url = jdbc:mysql://localhost:3306/javatechie
#spring.datasource.username = root
#spring.datasource.password = Password
#spring.jpa.hibernate.ddl-auto = update
#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment