SecurityConfig:
package com.sdkj.security.oauth.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder getPasswordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
//登录失败后跳转页面,必须为post请求
.failureForwardUrl("/toError")
//登录成功后跳转页面,必须为post请求
.successForwardUrl("/toMain")
//当发现/login时认为是登录,必须和表单提交的地址一样,接下来才会执行UserDetailsServiceImpl
.loginProcessingUrl("/login")
//自定义登录界面
.loginPage("/login.html");
http.authorizeRequests()
//error.html不需要进行认证
.antMatchers("/error.html").permitAll()
//login.html不需要进行认证
.antMatchers("/login.html").permitAll()
//所有请求都必须被认证,必须登录之后被访问
.anyRequest().authenticated();
//关闭csrf防护
http.csrf().disable();
}
}
TestController:
package com.sdkj.security.oauth.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder getPasswordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
//登录失败后跳转页面,必须为post请求
.failureForwardUrl("/toError")
//登录成功后跳转页面,必须为post请求
.successForwardUrl("/toMain")
//当发现/login时认为是登录,必须和表单提交的地址一样,接下来才会执行UserDetailsServiceImpl
.loginProcessingUrl("/login")
//自定义登录界面
.loginPage("/login.html");
http.authorizeRequests()
//error.html不需要进行认证
.antMatchers("/error.html").permitAll()
//login.html不需要进行认证
.antMatchers("/login.html").permitAll()
//所有请求都必须被认证,必须登录之后被访问
.anyRequest().authenticated();
//关闭csrf防护
http.csrf().disable();
}
}
UserDetailServiceImpl:
package com.sdkj.security.oauth.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
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.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@Service
public class UserDetailServiceImpl implements UserDetailsService {
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
String password = passwordEncoder.encode("123456");
return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal"));
}
}
error.html:
登录失败
登录失败
login.html:
登录
main.html:
登录
pom:
org.springframework.boot spring-boot-starter-security
整体项目结构:



