Skip to content

Commit c542425

Browse files
author
Davr bank
committed
Spring security
1 parent e8792ca commit c542425

File tree

158 files changed

+8993
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+8993
-27
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.7.2</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>uz.bepro</groupId>
12+
<artifactId>Spring-BaseAuth</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>Spring-BaseAuth</name>
15+
<description>Spring-BaseAuth</description>
16+
<properties>
17+
<java.version>11</java.version>
18+
<repackage.classifier/>
19+
<spring-native.version>0.12.1</spring-native.version>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-security</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.experimental</groupId>
28+
<artifactId>spring-native</artifactId>
29+
<version>${spring-native.version}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-devtools</artifactId>
35+
<scope>runtime</scope>
36+
<optional>true</optional>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<optional>true</optional>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.security</groupId>
50+
<artifactId>spring-security-test</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
54+
<!--security-->
55+
<dependency>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-starter-security</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.google.guava</groupId>
61+
<artifactId>guava</artifactId>
62+
<version>31.1-jre</version>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-starter-web</artifactId>
68+
</dependency>
69+
<!---->
70+
</dependencies>
71+
72+
<build>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.springframework.boot</groupId>
76+
<artifactId>spring-boot-maven-plugin</artifactId>
77+
<configuration>
78+
<excludes>
79+
<exclude>
80+
<groupId>org.projectlombok</groupId>
81+
<artifactId>lombok</artifactId>
82+
</exclude>
83+
</excludes>
84+
<classifier>${repackage.classifier}</classifier>
85+
<image>
86+
<builder>paketobuildpacks/builder:tiny</builder>
87+
<env>
88+
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
89+
</env>
90+
</image>
91+
</configuration>
92+
</plugin>
93+
<plugin>
94+
<groupId>org.springframework.experimental</groupId>
95+
<artifactId>spring-aot-maven-plugin</artifactId>
96+
<version>${spring-native.version}</version>
97+
<executions>
98+
<execution>
99+
<id>test-generate</id>
100+
<goals>
101+
<goal>test-generate</goal>
102+
</goals>
103+
</execution>
104+
<execution>
105+
<id>generate</id>
106+
<goals>
107+
<goal>generate</goal>
108+
</goals>
109+
</execution>
110+
</executions>
111+
</plugin>
112+
</plugins>
113+
</build>
114+
<repositories>
115+
<repository>
116+
<id>spring-releases</id>
117+
<name>Spring Releases</name>
118+
<url>https://repo.spring.io/release</url>
119+
<snapshots>
120+
<enabled>false</enabled>
121+
</snapshots>
122+
</repository>
123+
</repositories>
124+
<pluginRepositories>
125+
<pluginRepository>
126+
<id>spring-releases</id>
127+
<name>Spring Releases</name>
128+
<url>https://repo.spring.io/release</url>
129+
<snapshots>
130+
<enabled>false</enabled>
131+
</snapshots>
132+
</pluginRepository>
133+
</pluginRepositories>
134+
135+
<profiles>
136+
<profile>
137+
<id>native</id>
138+
<properties>
139+
<repackage.classifier>exec</repackage.classifier>
140+
<native-buildtools.version>0.9.13</native-buildtools.version>
141+
</properties>
142+
<dependencies>
143+
<dependency>
144+
<groupId>org.junit.platform</groupId>
145+
<artifactId>junit-platform-launcher</artifactId>
146+
<scope>test</scope>
147+
</dependency>
148+
</dependencies>
149+
<build>
150+
<plugins>
151+
<plugin>
152+
<groupId>org.graalvm.buildtools</groupId>
153+
<artifactId>native-maven-plugin</artifactId>
154+
<version>${native-buildtools.version}</version>
155+
<extensions>true</extensions>
156+
<executions>
157+
<execution>
158+
<id>test-native</id>
159+
<phase>test</phase>
160+
<goals>
161+
<goal>test</goal>
162+
</goals>
163+
</execution>
164+
<execution>
165+
<id>build-native</id>
166+
<phase>package</phase>
167+
<goals>
168+
<goal>build</goal>
169+
</goals>
170+
</execution>
171+
</executions>
172+
</plugin>
173+
</plugins>
174+
</build>
175+
</profile>
176+
</profiles>
177+
178+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package uz.bepro.springbaseauth;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
import java.io.Serializable;
7+
8+
@SpringBootApplication
9+
public class SpringBaseAuthApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringBaseAuthApplication.class, args);
13+
}
14+
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package uz.be.springrolebaseauth.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
import uz.be.springrolebaseauth.model.Student;
8+
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
/**
13+
* @project: secure-start
14+
* @Date: 12.08.2022
15+
* @author: H_Urunov
16+
**/
17+
@RestController
18+
@RequestMapping("/api/v1/student/")
19+
public class StudentController {
20+
//
21+
private static final List<Student> students = Arrays.asList(
22+
new Student(1, "James Bond"),
23+
new Student(2, "Lary Gaga"),
24+
new Student(3, "Faktor2"),
25+
new Student(4, "Anna ")
26+
);
27+
28+
@GetMapping(path = "{studentId}")
29+
public Student getStudent(@PathVariable("studentId") Integer studentId) {
30+
31+
return students
32+
.stream()
33+
.filter(student -> studentId.equals(student.getStudentId()))
34+
.findFirst()
35+
.orElseThrow(() -> new IllegalStateException("Student" + studentId));
36+
}
37+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package uz.be.springrolebaseauth.model;
2+
3+
import lombok.*;
4+
5+
/**
6+
* @project: secure-start
7+
* @Date: 12.08.2022
8+
* @author: H_Urunov
9+
**/
10+
@Getter
11+
@Setter
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
@ToString
15+
public class Student {
16+
//
17+
private Integer studentId;
18+
private String name;
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package uz.bepro.springbaseauth.security;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
10+
import org.springframework.security.core.userdetails.User;
11+
import org.springframework.security.core.userdetails.UserDetails;
12+
import org.springframework.security.core.userdetails.UserDetailsService;
13+
import org.springframework.security.crypto.password.PasswordEncoder;
14+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
15+
16+
import static uz.bepro.springbaseauth.security.ApplicationUserRole.*;
17+
18+
19+
/**
20+
* @project: secure-start
21+
* @Date: 12.08.2022
22+
* @author: H_Urunov
23+
**/
24+
@Configuration
25+
@EnableWebSecurity
26+
@EnableGlobalMethodSecurity(securedEnabled = true)
27+
public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {
28+
private final PasswordEncoder passwordEncoder;
29+
30+
@Autowired
31+
public ApplicationSecurityConfig(PasswordEncoder passwordEncoder) {
32+
this.passwordEncoder = passwordEncoder;
33+
}
34+
35+
@Override
36+
protected void configure(HttpSecurity http) throws Exception {
37+
http
38+
.csrf().disable()
39+
.authorizeRequests()
40+
.antMatchers("/", "index", "/css/*", "/js/*").permitAll()
41+
.antMatchers("/api/**").hasRole(ADMIN.name()) // managing role here based on enum categories.
42+
.anyRequest()
43+
.authenticated()
44+
.and()
45+
.httpBasic();
46+
}
47+
48+
@Override
49+
@Bean
50+
protected UserDetailsService userDetailsService() {
51+
// Permission User(s)
52+
UserDetails urunovUser = User.builder()
53+
.username("urunov")
54+
.password(passwordEncoder.encode("urunov1987"))
55+
.roles(ADMIN.name()) // ROLE_STUDENT
56+
.build();
57+
58+
UserDetails lindaUser = User.builder()
59+
.username("linda")
60+
.password(passwordEncoder.encode("linda333"))
61+
.authorities(STUDENT.name())
62+
.roles(STUDENT.name()) // ROLE_ADMIN
63+
.build();
64+
65+
UserDetails tomUser = User.builder()
66+
.username("tom")
67+
.password(passwordEncoder.encode("tom555"))
68+
.authorities(ADMINTRAINEE.name())
69+
// .authorities(ADMINTRAINEE.getGrantedAuthorities())
70+
.roles(ADMINTRAINEE.name()) // ROLE ADMINTRAINEE
71+
.build();
72+
//
73+
UserDetails hotamboyUser = User.builder()
74+
.username("hotam")
75+
.password(passwordEncoder.encode("hotamboy"))
76+
.authorities(ADMIN.name())
77+
// .authorities(ADMIN.getGrantedAuthorities())
78+
.roles(ADMIN.name()) // ROLE ADMIN
79+
.build();
80+
81+
return new InMemoryUserDetailsManager(
82+
urunovUser,
83+
lindaUser,
84+
tomUser,
85+
hotamboyUser
86+
87+
);
88+
89+
}
90+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.secure.sytem.securestart.security;
1+
package uz.bepro.springbaseauth.security;
22

33
/**
44
* @project: secure-start
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package com.secure.sytem.securestart.security;
2-
1+
package uz.bepro.springbaseauth.security;
32
import com.google.common.collect.Sets;
43
import org.springframework.security.core.authority.SimpleGrantedAuthority;
5-
64
import java.util.Set;
75
import java.util.stream.Collectors;
86

9-
import static com.secure.sytem.securestart.security.ApplicationUserPermission.*;
7+
import static uz.bepro.springbaseauth.security.ApplicationUserPermission.*;
8+
109

1110
/**
1211
* @project: secure-start
@@ -20,12 +19,12 @@ public enum ApplicationUserRole {
2019

2120
MANAGER(Sets.newHashSet(COURSE_READ, STUDENT_READ));
2221

23-
private final Set<ApplicationUserPermission> permissions;
22+
private final Set<ApplicationUserPermission> permissions;
2423

2524
ApplicationUserRole(Set<ApplicationUserPermission> permissions) {
2625
this.permissions = permissions;
2726
}
28-
27+
//
2928
// public Set<ApplicationUserPermission> getPermissions(){
3029
// return permissions;
3130
// }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.secure.sytem.securestart.security;
1+
package uz.bepro.springbaseauth.security;
22

33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;

0 commit comments

Comments
 (0)