1.引入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
2.配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1/security?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
username: root
password:
3.配置类
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyAccessDeniedHandler myAccessDeniedHandler;
@Autowired
private DataSource dataSource;
@Autowired
private PersistentTokenRepository persistentTokenRepository;
@Autowired
private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder getPassword(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/login")
.successForwardUrl("/toIndex")
.failureForwardUrl("/toError");
http.authorizeRequests()
.antMatchers("/css/**","js/**","**/*.png").permitAll()
.antMatchers("/login.html").permitAll()
.antMatchers("/error.html").permitAll()
.anyRequest().authenticated();
http.rememberMe()
//设置数据源
.tokenRepository(persistentTokenRepository)
// .rememberMeParameter()
//默认俩周
.tokenValiditySeconds(60)
.userDetailsService(userDetailsService);
http.csrf().disable();
}
@Bean
public PersistentTokenRepository persistentTokenRepository(){
JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
//设置数据源
jdbcTokenRepository.setDataSource(dataSource);
//自动建表 第一次使用 第二次报错
jdbcTokenRepository.setCreateTableOnStartup(true);
return jdbcTokenRepository;
}
}
4.login.html
<body>
<form action="/login" method="post">
Username : <input type="text" name="username"><br>
Password : <input type="password" name="password"><br>
Remermber me : <input type="checkbox" name="remember-me" value="true">
<input type="submit" value="login">
</form>
</body>
标签:功能,http,Autowired,private,rememberMe,html,mysql,login
From: https://www.cnblogs.com/lwx11111/p/17058176.html