Skip to content

Commit e694fb0

Browse files
committed
Few Changes
1 parent d309019 commit e694fb0

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.reuben.store.config;
22

33
import com.reuben.store.model.Customer;
4+
import com.reuben.store.repository.CustomerRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.security.core.userdetails.User;
47
import org.springframework.security.core.userdetails.UserDetails;
58
import org.springframework.security.core.userdetails.UserDetailsService;
69
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@@ -9,9 +12,15 @@
912
@Service
1013
public class AuthService implements UserDetailsService {
1114

15+
@Autowired
16+
private CustomerRepository customerRepository;
17+
1218
@Override
1319
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
14-
System.out.println(username);
15-
return null;
20+
Customer customer = customerRepository.fetchCustomerByEmail(username);
21+
if(customer == null) {
22+
throw new UsernameNotFoundException("Invalid Email ID");
23+
}
24+
return new org.springframework.security.core.userdetails.User(customer.getEmail_id(), customer.getPassword(),customer.getAuthorities());
1625
}
1726
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ private AuthenticationProvider authenticationProvider() {
4646
@Override
4747
protected void configure(HttpSecurity http) throws Exception {
4848
http.authorizeRequests()
49+
.antMatchers("/**").permitAll()
4950
.antMatchers("/customer").permitAll()
5051
.antMatchers("/vendor").permitAll()
52+
.antMatchers("/customer/{customer_id}").authenticated()
5153

52-
.antMatchers("/api/private/user/test").authenticated()
53-
.antMatchers("/api/private/admin/test").hasAuthority("ADMIN")// hasAnyAuthority("ADMIN","USER")
54+
.antMatchers("/product/purchase/{vendor_id}").hasAuthority("ADMIN")// hasAnyAuthority("ADMIN","USER")
5455
.and()
5556
.httpBasic();
5657
http.csrf().disable();

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.reuben.store.model;
22

33
import org.springframework.security.core.GrantedAuthority;
4+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
45
import org.springframework.security.core.userdetails.UserDetails;
56

67
import javax.persistence.*;
8+
import java.util.ArrayList;
79
import java.util.Collection;
810
import java.util.List;
911

@@ -68,7 +70,10 @@ public void setPassword(String password) {
6870

6971
@Override
7072
public Collection<? extends GrantedAuthority> getAuthorities() {
71-
return null;
73+
List<GrantedAuthority> list = new ArrayList<>();
74+
SimpleGrantedAuthority sga = new SimpleGrantedAuthority(role);
75+
list.add(sga);
76+
return list;
7277
}
7378

7479

0 commit comments

Comments
 (0)