This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#141] add services, password encoder
- Loading branch information
Showing
17 changed files
with
1,395 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package de.bonndan.nivio; | ||
|
||
import de.bonndan.nivio.database.User; | ||
import de.bonndan.nivio.database.UserRepository; | ||
import de.bonndan.nivio.appuser.AppUser; | ||
import de.bonndan.nivio.appuser.AppUserRole; | ||
import de.bonndan.nivio.appuser.AppUserRepository; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
@@ -17,9 +18,9 @@ public static void main(String[] args) { | |
} | ||
|
||
@Bean | ||
CommandLineRunner commandLineRunner(UserRepository userRepository) { | ||
CommandLineRunner commandLineRunner(AppUserRepository userRepository) { | ||
return args -> { | ||
User mary = new User("Mary", "[email protected]"); | ||
AppUser mary = new AppUser("Mary", "mary88", "[email protected]", "secret123", AppUserRole.USER, false, true ); | ||
userRepository.save(mary); | ||
}; | ||
} | ||
|
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,135 @@ | ||
package de.bonndan.nivio.appuser; | ||
|
||
import lombok.*; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import javax.persistence.*; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
@Getter | ||
@Setter | ||
@EqualsAndHashCode | ||
@NoArgsConstructor | ||
@Entity(name = "AppUser") | ||
@Table( | ||
name = "user", | ||
uniqueConstraints = { | ||
@UniqueConstraint(name = "user_email_unique", | ||
columnNames = "email") | ||
} | ||
) | ||
public class AppUser implements UserDetails { | ||
|
||
@SequenceGenerator( | ||
name = "user_sequence", | ||
sequenceName = "user_sequence", | ||
allocationSize = 1 | ||
) | ||
@Id | ||
@GeneratedValue( | ||
strategy = GenerationType.SEQUENCE, | ||
generator = "user_sequence" | ||
) | ||
@Column( | ||
name = "id", | ||
updatable = false | ||
) | ||
private Long id; | ||
|
||
@Column( | ||
name = "name", | ||
nullable = false, | ||
columnDefinition = "TEXT" | ||
) | ||
private String name; | ||
|
||
|
||
@Column( | ||
name = "user_name", | ||
nullable = false, | ||
columnDefinition = "TEXT" | ||
) | ||
private String userName; | ||
|
||
@Column( | ||
name = "email", | ||
nullable = false, | ||
columnDefinition = "TEXT" | ||
) | ||
private String email; | ||
|
||
@Column( | ||
name = "password", | ||
nullable = false, | ||
columnDefinition = "TEXT" | ||
) | ||
private String password; | ||
|
||
|
||
@Column( | ||
name = "role", | ||
nullable = false, | ||
columnDefinition = "TEXT" | ||
) | ||
@Enumerated(EnumType.STRING) | ||
private AppUserRole appUserRole; | ||
|
||
private Boolean locked; | ||
private Boolean enabled; | ||
|
||
public AppUser(String name, | ||
String userName, | ||
String email, | ||
String password, | ||
AppUserRole appUserRole, | ||
Boolean locked, | ||
Boolean enabled) { | ||
this.name = name; | ||
this.userName = userName; | ||
this.email = email; | ||
this.password = password; | ||
this.appUserRole = appUserRole; | ||
this.locked = locked; | ||
this.enabled = enabled; | ||
} | ||
|
||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(appUserRole.name()); | ||
return Collections.singletonList(authority); | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return userName; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return !locked; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/de/bonndan/nivio/appuser/AppUserRepository.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,15 @@ | ||
package de.bonndan.nivio.appuser; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
@Transactional(readOnly = true) | ||
public interface AppUserRepository extends JpaRepository<AppUser, Long> { | ||
|
||
Optional<AppUser> findByEmail(String email); | ||
|
||
} |
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,6 @@ | ||
package de.bonndan.nivio.appuser; | ||
|
||
public enum AppUserRole { | ||
USER, | ||
ADMIN | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/de/bonndan/nivio/appuser/AppUserService.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,21 @@ | ||
package de.bonndan.nivio.appuser; | ||
|
||
import lombok.AllArgsConstructor; | ||
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; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
public class AppUserService implements UserDetailsService { | ||
|
||
private static final String USER_NOT_FOUND = "User with email %s not found."; | ||
private final AppUserRepository appUserRepository; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { | ||
return appUserRepository.findByEmail(email) | ||
.orElseThrow(() -> new UsernameNotFoundException(String.format(USER_NOT_FOUND, email))); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/main/java/de/bonndan/nivio/registration/RegistrationController.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,18 @@ | ||
package de.bonndan.nivio.registration; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping(path="/registration") | ||
@AllArgsConstructor | ||
public class RegistrationController { | ||
|
||
private RegistrationService registrationService; | ||
|
||
public String register(@RequestBody RegistrationRequest registrationRequest) { | ||
return registrationService.register(registrationRequest); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/de/bonndan/nivio/registration/RegistrationRequest.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,19 @@ | ||
package de.bonndan.nivio.registration; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@EqualsAndHashCode | ||
@ToString | ||
public class RegistrationRequest { | ||
|
||
private final String name; | ||
private final String userName; | ||
private final String email; | ||
private final String password; | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/de/bonndan/nivio/registration/RegistrationService.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,12 @@ | ||
package de.bonndan.nivio.registration; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class RegistrationService { | ||
|
||
|
||
public String register(RegistrationRequest registrationRequest) { | ||
return "it works"; | ||
} | ||
} |
Oops, something went wrong.