Skip to content

Commit

Permalink
Upgrade admin login function
Browse files Browse the repository at this point in the history
  • Loading branch information
KADGang committed May 18, 2022
1 parent 17e10a6 commit 267c41a
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ spring:
password: #数据库密码
driver-class-name: com.mysql.cj.jdbc.Driver

qiusheng:
greeting: Welcome to QiuSheng Server #自定义服务器欢迎语

mybatis:
mapper-locations: classpath:/mapping/*.xml
#mybatis的映射器文件目录
Expand Down
3 changes: 3 additions & 0 deletions README_ENG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ spring:
password: #password of your database server
driver-class-name: com.mysql.cj.jdbc.Driver

qiusheng:
greeting: Welcome to QiuSheng Server #Customize greeting words

mybatis:
mapper-locations: classpath:/mapping/*.xml
```
Expand Down
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;
}
}
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<spring-boot.version>2.6.3</spring-boot.version>
<dubbo.version>3.0.5</dubbo.version>
<zookeeper.version>3.6.3</zookeeper.version>
<spring-security.version>5.6.3</spring-security.version>
</properties>

<dependencyManagement>
Expand All @@ -35,4 +36,11 @@
</dependencies>
</dependencyManagement>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>
7 changes: 1 addition & 6 deletions qiusheng-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-security.version>5.6.3</spring-security.version>

</properties>

<dependencies>
Expand Down Expand Up @@ -78,11 +78,6 @@
<version>2.7</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
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();
}
}
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);
}
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;
}
}
9 changes: 9 additions & 0 deletions qiusheng-core/src/main/resources/mapping/AdminMapper.xml
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>
15 changes: 10 additions & 5 deletions qiusheng-core/src/main/resources/sql/qiusheng_core.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ create table if not exists events
constraint events_song_sid_fk
foreign key (sid) references song (sid)
);





create table if not exists admin
(
id int auto_increment,
username varchar(30) not null,
password varchar(30) not null,
enable boolean default true not null,
locked boolean default false not null,
constraint admin_pk
primary key (id)
);

0 comments on commit 267c41a

Please sign in to comment.