-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Authorization using Authorities,Roles
- Loading branch information
Showing
22 changed files
with
341 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 48 additions & 19 deletions
67
section7/springsecsection7/src/main/java/com/eazybytes/controller/LoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,60 @@ | ||
package com.eazybytes.controller; | ||
|
||
|
||
import java.security.Principal; | ||
import java.util.List; | ||
|
||
import com.eazybytes.model.Customer; | ||
import com.eazybytes.repository.CustomerRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.eazybytes.model.Customer; | ||
import com.eazybytes.repository.CustomerRepository; | ||
import java.sql.Date; | ||
import java.util.List; | ||
|
||
@RestController | ||
public class LoginController { | ||
|
||
@Autowired | ||
private CustomerRepository customerRepository; | ||
|
||
@RequestMapping("/user") | ||
public Customer getUserDetailsAfterLogin(Principal user) { | ||
List<Customer> customers = customerRepository.findByEmail(user.getName()); | ||
if (customers.size() > 0) { | ||
return customers.get(0); | ||
}else { | ||
return null; | ||
} | ||
|
||
} | ||
@Autowired | ||
private CustomerRepository customerRepository; | ||
|
||
@Autowired | ||
private PasswordEncoder passwordEncoder; | ||
|
||
@PostMapping("/register") | ||
public ResponseEntity<String> registerUser(@RequestBody Customer customer) { | ||
Customer savedCustomer = null; | ||
ResponseEntity response = null; | ||
try { | ||
String hashPwd = passwordEncoder.encode(customer.getPwd()); | ||
customer.setPwd(hashPwd); | ||
customer.setCreateDt(String.valueOf(new Date(System.currentTimeMillis()))); | ||
savedCustomer = customerRepository.save(customer); | ||
if (savedCustomer.getId() > 0) { | ||
response = ResponseEntity | ||
.status(HttpStatus.CREATED) | ||
.body("Given user details are successfully registered"); | ||
} | ||
} catch (Exception ex) { | ||
response = ResponseEntity | ||
.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.body("An exception occured due to " + ex.getMessage()); | ||
} | ||
return response; | ||
} | ||
|
||
@RequestMapping("/user") | ||
public Customer getUserDetailsAfterLogin(Authentication authentication) { | ||
List<Customer> customers = customerRepository.findByEmail(authentication.getName()); | ||
if (customers.size() > 0) { | ||
return customers.get(0); | ||
} else { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.