Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 89e7eb1

Browse files
committed
Updated Code with SECURITY Features
1 parent d4055c7 commit 89e7eb1

File tree

5 files changed

+125
-9
lines changed

5 files changed

+125
-9
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.reuben.store.config;
2+
3+
import com.reuben.store.model.Customer;
4+
import org.springframework.security.core.userdetails.UserDetails;
5+
import org.springframework.security.core.userdetails.UserDetailsService;
6+
import org.springframework.security.core.userdetails.UsernameNotFoundException;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service
10+
public class AuthService implements UserDetailsService {
11+
12+
@Override
13+
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
14+
System.out.println(username);
15+
return null;
16+
}
17+
}

‎src/main/java/com/reuben/store/config/SecurityConfig.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.security.authentication.AuthenticationProvider;
8+
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
79
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
10+
import org.springframework.security.config.annotation.authentication.configurers.userdetails.DaoAuthenticationConfigurer;
811
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
912
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1013
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@@ -18,19 +21,34 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
1821
@Autowired //Could not autowire. No beans of 'PasswordEncoder' type found.
1922
private PasswordEncoder passwordEncoder;
2023

24+
@Autowired
25+
private AuthService authService;
26+
2127
@Override
2228
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
23-
auth.inMemoryAuthentication()
24-
.passwordEncoder(passwordEncoder)
25-
.withUser("reuben").password(passwordEncoder.encode("1234")).authorities("ADMIN")
26-
.and()
27-
.withUser("rhea").password(passwordEncoder.encode("rhea")).authorities("USER");
29+
// auth.inMemoryAuthentication()
30+
// .passwordEncoder(passwordEncoder)
31+
// .withUser("reuben").password(passwordEncoder.encode("1234")).authorities("ADMIN")
32+
// .and()
33+
// .withUser("rhea").password(passwordEncoder.encode("rhea")).authorities("USER");
34+
35+
auth.authenticationProvider(authenticationProvider());
2836
}
2937

38+
private AuthenticationProvider authenticationProvider() {
39+
DaoAuthenticationProvider dao = new DaoAuthenticationProvider();
40+
dao.setPasswordEncoder(passwordEncoder);
41+
dao.setUserDetailsService(authService);
42+
return dao;
43+
}
44+
45+
3046
@Override
3147
protected void configure(HttpSecurity http) throws Exception {
3248
http.authorizeRequests()
33-
.antMatchers("/api/public/test").permitAll()
49+
.antMatchers("/customer").permitAll()
50+
.antMatchers("/vendor").permitAll()
51+
3452
.antMatchers("/api/private/user/test").authenticated()
3553
.antMatchers("/api/private/admin/test").hasAuthority("ADMIN")// hasAnyAuthority("ADMIN","USER")
3654
.and()

‎src/main/java/com/reuben/store/controller/MainController.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import com.reuben.store.model.*;
77
import com.reuben.store.repository.*;
88
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.context.annotation.Bean;
910
import org.springframework.http.HttpStatus;
1011
import org.springframework.http.ResponseEntity;
12+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
13+
import org.springframework.security.crypto.password.PasswordEncoder;
1114
import org.springframework.web.bind.annotation.*;
1215

1316
import java.time.LocalDate;
@@ -34,26 +37,51 @@ public class MainController {
3437
@Autowired
3538
private PurchaseRepository purchaseRepository;
3639

40+
@Autowired //Could not autowire. No beans of 'PasswordEncoder' type found.
41+
private PasswordEncoder passwordEncoder;
42+
43+
44+
3745
@PostMapping("/vendor")
3846
public Vendor addVendor(@RequestBody Vendor vendor){
3947
// Saving the Vendor Details
4048
Vendor v = vendorRepository.save(vendor);
4149
return v;
4250
}
4351

52+
4453
@PostMapping("/customer")
4554
public Customer addCustomer(
4655
@RequestBody Customer customer
4756

4857
){
4958
// Saving the Vendor Details
59+
String password_recived = customer.getPassword();
60+
customer.setPassword(passwordEncoder.encode(password_recived));
61+
Customer c = customerRepository.save(customer);
62+
// System.out.println(c.getId());
63+
return c;
64+
65+
66+
}
67+
68+
@PostMapping("/customer/login")
69+
public Customer customerLogin(
70+
@RequestBody Customer customer
71+
72+
){
73+
// Saving the Vendor Details
74+
String password_recived = customer.getPassword();
75+
customer.setPassword(passwordEncoder.encode(password_recived));
5076
Customer c = customerRepository.save(customer);
5177
// System.out.println(c.getId());
5278
return c;
5379

5480

5581
}
5682

83+
84+
5785
@PostMapping("/customer/{customer_id}")
5886
public Customer updateProfile(@RequestBody Profile profile,
5987
@PathVariable("customer_id") Long customer_id) {
@@ -69,6 +97,7 @@ public Customer updateProfile(@RequestBody Profile profile,
6997

7098
}
7199

100+
72101
@PostMapping("addproduct/{vendor_id}")
73102
public Product AddProduct(@PathVariable("vendor_id") Long vendor_id,
74103
@RequestBody Product product){

‎src/main/java/com/reuben/store/model/Customer.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
package com.reuben.store.model;
22

3+
import org.springframework.security.core.GrantedAuthority;
4+
import org.springframework.security.core.userdetails.UserDetails;
5+
36
import javax.persistence.*;
7+
import java.util.Collection;
48
import java.util.List;
59

610
@Entity
7-
public class Customer {
11+
public class Customer implements UserDetails {
12+
813
@Id
9-
@GeneratedValue(strategy= GenerationType.AUTO)
14+
@GeneratedValue(strategy= GenerationType.AUTO)
1015
private Long id;
1116

1217
@Column(length = 100)
1318
private String email_id;
1419

15-
@Column(length = 100)
20+
@Column(length = 1000)
1621
private String password;
1722

23+
@Column(length = 100)
24+
private String role;
25+
1826
@OneToOne
1927
private Profile profile;
2028

@@ -26,6 +34,14 @@ public void setProfile(Profile profile) {
2634
this.profile = profile;
2735
}
2836

37+
public String getRole() {
38+
return role;
39+
}
40+
41+
public void setRole(String role) {
42+
this.role = role;
43+
}
44+
2945
public Long getId() {
3046
return id;
3147
}
@@ -50,4 +66,36 @@ public void setPassword(String password) {
5066
this.password = password;
5167
}
5268

69+
@Override
70+
public Collection<? extends GrantedAuthority> getAuthorities() {
71+
return null;
72+
}
73+
74+
75+
@Override
76+
public String getUsername() {
77+
return null;
78+
}
79+
80+
@Override
81+
public boolean isAccountNonExpired() {
82+
return true;
83+
}
84+
85+
@Override
86+
public boolean isAccountNonLocked() {
87+
return true;
88+
}
89+
90+
@Override
91+
public boolean isCredentialsNonExpired() {
92+
return true;
93+
}
94+
95+
@Override
96+
public boolean isEnabled() {
97+
return true;
98+
}
99+
100+
53101
}

‎src/main/java/com/reuben/store/repository/CustomerRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import com.reuben.store.model.Customer;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
56

67
public interface CustomerRepository extends JpaRepository<Customer, Long> {
78

9+
@Query("SELECT C from Customer C WHERE C.email_id = ?1")
10+
Customer fetchByUsername(String username);
11+
812
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /