-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cubewhy
committed
Oct 3, 2024
1 parent
8ceae04
commit c004ca2
Showing
9 changed files
with
133 additions
and
13 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package fuck.manthe.nmsl.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Entity | ||
public class Admin { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private long id; | ||
|
||
private String username; | ||
private String password; | ||
private String role; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/fuck/manthe/nmsl/entity/UserDetailsImpl.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,27 @@ | ||
package fuck.manthe.nmsl.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class UserDetailsImpl implements UserDetails { | ||
private String username; | ||
private String password; | ||
private String role; | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return List.of(new SimpleGrantedAuthority(role)); | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/fuck/manthe/nmsl/repository/AdminRepository.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 fuck.manthe.nmsl.repository; | ||
|
||
import fuck.manthe.nmsl.entity.Admin; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface AdminRepository extends JpaRepository<Admin, Long> { | ||
Optional<Admin> 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,7 @@ | ||
package fuck.manthe.nmsl.service; | ||
|
||
import fuck.manthe.nmsl.entity.Admin; | ||
|
||
public interface AdminService { | ||
Admin findByUsername(String username); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/fuck/manthe/nmsl/service/impl/AdminServiceImpl.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 fuck.manthe.nmsl.service.impl; | ||
|
||
import fuck.manthe.nmsl.entity.Admin; | ||
import fuck.manthe.nmsl.repository.AdminRepository; | ||
import fuck.manthe.nmsl.service.AdminService; | ||
import jakarta.annotation.Resource; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AdminServiceImpl implements AdminService { | ||
@Resource | ||
AdminRepository adminRepository; | ||
|
||
@Override | ||
public Admin findByUsername(String username) { | ||
return adminRepository.findByUsername(username).orElse(null); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/fuck/manthe/nmsl/service/impl/UserDetailsServiceImpl.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,27 @@ | ||
package fuck.manthe.nmsl.service.impl; | ||
|
||
import fuck.manthe.nmsl.entity.Admin; | ||
import fuck.manthe.nmsl.entity.UserDetailsImpl; | ||
import fuck.manthe.nmsl.service.AdminService; | ||
import jakarta.annotation.Resource; | ||
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 | ||
public class UserDetailsServiceImpl implements UserDetailsService { | ||
@Resource | ||
AdminService adminService; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
Admin admin = adminService.findByUsername(username); | ||
if (admin == null) return null; | ||
return UserDetailsImpl.builder() | ||
.username(admin.getUsername()) | ||
.password(admin.getPassword()) | ||
.role(admin.getRole()) | ||
.build(); | ||
} | ||
} |