-
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.
- Loading branch information
Showing
10 changed files
with
149 additions
and
14 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
56 changes: 56 additions & 0 deletions
56
api-entity/src/main/java/org/pockettech/qiusheng/entity/userrole/Admin.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,56 @@ | ||
package org.pockettech.qiusheng.entity.userrole; | ||
|
||
import lombok.Data; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
@Data | ||
public class Admin implements UserDetails { | ||
private Integer id; | ||
private String username; | ||
private String password; | ||
private boolean enable; | ||
private boolean locked; | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
List<SimpleGrantedAuthority> authorities = new ArrayList<>(); | ||
authorities.add(new SimpleGrantedAuthority("admin")); | ||
return authorities; | ||
} | ||
|
||
@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 enable; | ||
} | ||
} |
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
20 changes: 17 additions & 3 deletions
20
qiusheng-core/src/main/java/org/pockettech/qiusheng/config/AdminSecurityConfig.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,22 +1,36 @@ | ||
package org.pockettech.qiusheng.config; | ||
|
||
import org.pockettech.qiusheng.impl.admin.AdminDetailService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class AdminSecurityConfig extends WebSecurityConfigurerAdapter { | ||
@Autowired | ||
private AdminDetailService adminDetailService; | ||
|
||
@Override | ||
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | ||
auth.userDetailsService(adminDetailService).passwordEncoder(new BCryptPasswordEncoder()); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
http.authorizeRequests() | ||
.antMatchers("/", "/api/**", "/resource/**") | ||
.permitAll() | ||
.anyRequest() | ||
.authenticated() | ||
.authenticated() | ||
.and() | ||
.formLogin(); | ||
//.loginPage("") TODO:实现自定义登录 | ||
.formLogin() | ||
.loginProcessingUrl("/admin/login") | ||
// .loginPage("/login") //TODO:实现自定义登录 | ||
.permitAll(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
qiusheng-core/src/main/java/org/pockettech/qiusheng/dao/AdminDao.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 org.pockettech.qiusheng.dao; | ||
|
||
import org.pockettech.qiusheng.entity.userrole.Admin; | ||
|
||
public interface AdminDao { | ||
public Admin loadAdminByUsername(String username); | ||
} |
35 changes: 35 additions & 0 deletions
35
qiusheng-core/src/main/java/org/pockettech/qiusheng/impl/admin/AdminDetailService.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,35 @@ | ||
package org.pockettech.qiusheng.impl.admin; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.pockettech.qiusheng.dao.AdminDao; | ||
import org.pockettech.qiusheng.entity.userrole.Admin; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Controller; | ||
|
||
import javax.annotation.Resource; | ||
|
||
@Slf4j | ||
@Controller | ||
public class AdminDetailService implements UserDetailsService { | ||
@Resource | ||
private AdminDao adminDao; | ||
private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
Admin admin = adminDao.loadAdminByUsername(username); | ||
if (admin == null) { | ||
throw new UsernameNotFoundException("Admin account not found."); | ||
} | ||
|
||
String encodePassword = passwordEncoder.encode(admin.getPassword()); | ||
log.info("Password(encrypted):" + encodePassword); | ||
admin.setPassword(encodePassword); | ||
return admin; | ||
} | ||
} |
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,9 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!DOCTYPE mapper | ||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
<mapper namespace="org.pockettech.qiusheng.dao.AdminDao"> | ||
<select id="loadAdminByUsername" parameterType="string" resultType="org.pockettech.qiusheng.entity.userrole.Admin"> | ||
select * from `admin` where `username` = #{username} | ||
</select> | ||
</mapper> |
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