-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom user details service with a registration page
- Loading branch information
1 parent
3266c90
commit 996a81c
Showing
9 changed files
with
232 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package tacos; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Entity | ||
@Data | ||
@NoArgsConstructor(access=AccessLevel.PRIVATE, force=true) | ||
@RequiredArgsConstructor | ||
@Table(name="RegisteredUsers") | ||
public class User implements UserDetails{ | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
@Id | ||
@GeneratedValue(strategy=GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private final String username; | ||
private final String password; | ||
private final String fullname; | ||
private final String street; | ||
private final String city; | ||
private final String state; | ||
private final String zip; | ||
private final String phoneNumber; | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package tacos.data; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import tacos.User; | ||
|
||
public interface UserRepository extends CrudRepository<User, Long>{ | ||
|
||
User findByUsername(String username); | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tacos.security; | ||
|
||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import tacos.data.UserRepository; | ||
|
||
@Controller | ||
@RequestMapping("/register") | ||
public class RegistrationController { | ||
|
||
private UserRepository userRepo; | ||
private PasswordEncoder passwordEncoder; | ||
|
||
public RegistrationController(UserRepository userRepo, PasswordEncoder passwordEncoder) { | ||
this.userRepo = userRepo; | ||
this.passwordEncoder = passwordEncoder; | ||
} | ||
|
||
@GetMapping | ||
public String registerForm() { | ||
return "registration"; | ||
} | ||
|
||
@PostMapping | ||
public String processingRegistration(RegistrationForm form) { | ||
userRepo.save(form.toUser(passwordEncoder)); | ||
return "redirect:/login"; | ||
|
||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package tacos.security; | ||
|
||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
import lombok.Data; | ||
import tacos.User; | ||
|
||
@Data | ||
public class RegistrationForm { | ||
|
||
private String username; | ||
private String password; | ||
private String fullname; | ||
private String street; | ||
private String city; | ||
private String state; | ||
private String zip; | ||
private String phone; | ||
|
||
public User toUser(PasswordEncoder passwordEncoder) { | ||
return new User( | ||
username, passwordEncoder.encode(password), | ||
fullname, street, city, state, zip, phone); | ||
} | ||
|
||
} |
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
31 changes: 31 additions & 0 deletions
31
src/main/java/tacos/security/UserRepositoryUserDetailsService.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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package tacos.security; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import tacos.User; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
import tacos.data.UserRepository; | ||
|
||
@Service | ||
public class UserRepositoryUserDetailsService implements UserDetailsService{ | ||
|
||
private UserRepository userRepo; | ||
|
||
@Autowired | ||
public UserRepositoryUserDetailsService(UserRepository userRepo) { | ||
this.userRepo = userRepo; | ||
} | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
User user = userRepo.findByUsername(username); | ||
if(user != null) { | ||
return user; | ||
} | ||
throw new UsernameNotFoundException("User '" + username + "' not found"); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<!DOCTYPE html> | ||
<html xmlns="http://www.w3.org/1999/xhtml" | ||
xmlns:th="http://www.thymeleaf.org"> | ||
<head> | ||
<title>Taco Cloud</title> | ||
</head> | ||
|
||
<body> | ||
<h1>Register</h1> | ||
<img th:src="@{/images/TacoCloud.png}"/> | ||
|
||
<form method="POST" th:action="@{/register}" id="registerForm"> | ||
<label for="username">Username: </label> | ||
<input type="text" name="username"/><br/> | ||
|
||
<label for="password">Password: </label> | ||
<input type="password" name="password"/><br/> | ||
|
||
<label for="confirm">Confirm password: </label> | ||
<input type="password" name="confirm"/><br/> | ||
|
||
<label for="fullname">Full name: </label> | ||
<input type="text" name="fullname"/><br/> | ||
|
||
<label for="street">Street: </label> | ||
<input type="text" name="street"/><br/> | ||
|
||
<label for="city">City: </label> | ||
<input type="text" name="city"/><br/> | ||
|
||
<label for="state">State: </label> | ||
<input type="text" name="state"/><br/> | ||
|
||
<label for="zip">Zip: </label> | ||
<input type="text" name="zip"/><br/> | ||
|
||
<label for="phone">Phone: </label> | ||
<input type="text" name="phone"/><br/> | ||
|
||
<input type="submit" value="Register"/> | ||
</form> | ||
</body> | ||
</html> |
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