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
Showing
9 changed files
with
178 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
端口:8081 | ||
[http://localhost:8081](http://localhost:8081]) | ||
|
||
表单登录地址:http://localhost:8081/login.html | ||
表单登录地址:http://localhost:8081/login.html | ||
|
||
账号:admin,user | ||
|
||
密码:1234 |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/al/_01springsecuritydemo01/dao/UserDao.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,10 @@ | ||
package com.al._01springsecuritydemo01.dao; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.al._01springsecuritydemo01.entity.User; | ||
|
||
@Repository | ||
public interface UserDao extends JpaRepository<User, Long> { | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/al/_01springsecuritydemo01/entity/User.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,80 @@ | ||
package com.al._01springsecuritydemo01.entity; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import javax.persistence.Transient; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Table(name = "users") | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class User implements UserDetails { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column | ||
private Long userId; | ||
|
||
@Column(name = "user_name") | ||
private String userName; | ||
|
||
@Column(name = "password") | ||
private String password; | ||
|
||
@Column(name = "status") | ||
private String status; | ||
|
||
@Column(name = "roles") | ||
private String roles; | ||
|
||
@Transient | ||
private List<GrantedAuthority> authorities; | ||
|
||
public void setAuthorities(List<GrantedAuthority> authorities) { | ||
this.authorities = authorities; | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return this.authorities; | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return this.userName; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/al/_01springsecuritydemo01/service/UserDetailService.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 com.al._01springsecuritydemo01.service; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.authority.AuthorityUtils; | ||
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 com.al._01springsecuritydemo01.entity.User; | ||
|
||
@Service | ||
public class UserDetailService implements UserDetailsService { | ||
@Autowired | ||
private UserService userService; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
User user = userService.selectUserByUserName(username); | ||
if (user == null) { | ||
throw new UsernameNotFoundException("用户" + user.getUsername() + "不存在"); | ||
} | ||
// 设置权限 | ||
user.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRoles())); | ||
return user; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/al/_01springsecuritydemo01/service/UserService.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,7 @@ | ||
package com.al._01springsecuritydemo01.service; | ||
|
||
import com.al._01springsecuritydemo01.entity.User; | ||
|
||
public interface UserService { | ||
public User selectUserByUserName(String username); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/al/_01springsecuritydemo01/service/UserServiceImpl.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,24 @@ | ||
package com.al._01springsecuritydemo01.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Example; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.al._01springsecuritydemo01.dao.UserDao; | ||
import com.al._01springsecuritydemo01.entity.User; | ||
|
||
@Service | ||
public class UserServiceImpl implements UserService { | ||
@Autowired | ||
private UserDao userDao; | ||
|
||
@Override | ||
public User selectUserByUserName(String username) { | ||
User user = new User(); | ||
user.setUserName(username); | ||
List<User> list = userDao.findAll(Example.of(user)); | ||
return list.isEmpty() ? null : list.get(0); | ||
} | ||
} |
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 +1,5 @@ | ||
server.port=8081 | ||
server.port=8081 | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
spring.datasource.username=root | ||
spring.datasource.password=1234 | ||
spring.datasource.url=jdbc:mysql://localhost:3306/jwt_demo?serverTimezone=GMT%2B8&characterEncoding=utf-8 |