Skip to content

Commit

Permalink
实现记住我的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglufei committed Apr 11, 2022
1 parent 2aaac16 commit a5e7e37
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .idea/libraries/Maven__com_zaxxer_HikariCP_4_0_3.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__mysql_mysql_connector_java_8_0_28.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_mybatis_mybatis_3_5_7.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_mybatis_mybatis_spring_2_0_6.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_springframework_spring_jdbc_5_3_18.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_springframework_spring_tx_5_3_18.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--test组件-->
<dependency>
<groupId>org.testng</groupId>
Expand Down
9 changes: 9 additions & 0 deletions springsecurity_demo.iml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@
<orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.18" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.18" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.28" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.5.12" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:4.0.3" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.3.18" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.3.18" level="project" />
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:2.2.0" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.5.7" level="project" />
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:2.0.6" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.28" level="project" />
<orderEntry type="library" name="Maven: org.testng:testng:7.5" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:3.0.1" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.36" level="project" />
Expand Down
43 changes: 42 additions & 1 deletion src/main/java/com/uin/config/SpringSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.uin.handler.MyAccessDeniedHandler;
import com.uin.handler.MyAuthenticationSuccessHandler;
import com.uin.handler.MyForwardAuthenticationFailureHandler;
import com.uin.service.impl.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -14,6 +15,10 @@
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

/**
* @author wanglufei
Expand All @@ -25,6 +30,13 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private MyAccessDeniedHandler myAccessDeniedHandler;
@Autowired
UserDetailsServiceImpl userDetailsService;
@Autowired
DataSource dataSource;
@Autowired
PersistentTokenRepository persistentTokenRepository;

/**
* 自定义登陆之后的逻辑
* 1.自定义登陆页面
Expand Down Expand Up @@ -122,9 +134,21 @@ protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();


//自定义403权限异常
//自定义403 权限异常
http.exceptionHandling()
.accessDeniedHandler(myAccessDeniedHandler);

//rememberMe 功能
//当用户选择记住我的时候,它底层会把用户的数据信息放到数据库中
http.rememberMe()
//自定义过期时间 s
//.tokenValiditySeconds(600000)
//自定义参数
//.rememberMeParameter("remember")
//自定义的UserDetailsServiceImpl
.userDetailsService(userDetailsService)
//持久层对象
.tokenRepository(persistentTokenRepository);
}

/**
Expand All @@ -139,4 +163,21 @@ protected void configure(HttpSecurity http) throws Exception {
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

/**
* 配置数据存到数据库
*
* @return org.springframework.security.web.authentication.rememberme.PersistentTokenRepository
* @author wanglufei
* @date 2022/4/11 3:01 PM
*/
@Bean
public PersistentTokenRepository getPersistentTokenRepository() {
JdbcTokenRepositoryImpl jdbc = new JdbcTokenRepositoryImpl();
//设置数据源
jdbc.setDataSource(dataSource);
//自动建表 第一次建表改为true 第二次要注释
jdbc.setCreateTableOnStartup(true);
return jdbc;
}
}
7 changes: 7 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ spring:
#???????
servlet:
path: /uin

datasource:
password: 12345678
url: jdbc:mysql://localhost:3306/security?useUnicode=true&characterEncoding=UTF-8&serverTimezone
=Asia/Shanghai
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
1 change: 1 addition & 0 deletions src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<form action="/login" method="post">
用户名:<input type="text" name="username" placeholder="请输入用户名">
密码:<input type="password" name="password" placeholder="请输入密码">
记住我:<input type="checkbox" name="remember-me" value="true">
<button type="submit">登陆</button>
</form>
</body>
Expand Down

0 comments on commit a5e7e37

Please sign in to comment.