首页>代码>springboot2+spring security+thymeleaf实现用户权限验证登陆,记住密码功能>/SpringSecurity_Demo1/src/main/java/com/shang/config/MySecurityConfig.java
package com.shang.config;
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;
import org.springframework.util.AntPathMatcher;
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter{
//定制请求的授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("VIP1")
.antMatchers("/level2/**").hasRole("VIP2")
.antMatchers("/level3/**").hasRole("VIP3");
//开启登陆功能,没有权限,没有登陆就会到登陆页面
/**
* 1./login请求来到登陆页
* 2.重定向到/login?error表示登陆失败
* 3.更多功能规则
* 4.默认post形式的/login 代表登陆
* 5.usernameParameter 设置登陆用户名参数名,passwordParameter 设置登陆密码参数名
* 6.一旦定制loginPage,那么loginPage的post请求就是登陆
* 7.默认的登陆成功跳转页面是/,也可以通过defaultSuccessUrl设定登陆成功跳转页面
* 没有loginPage()的话,登录页面默认为/login, loginPage()可以指定登录页
*/
http.formLogin().usernameParameter("user").passwordParameter("pwd").loginPage("/userLogin").defaultSuccessUrl("/");
/**
* 开启自动配置的注销功能
* 1.访问/logout表示用户注销,清空session
* 2.注销成功会定向到/login?logout,可以用logoutSuccessUrl()定制
*/
http.logout().logoutSuccessUrl("/");
/**
* 开启记住我功能
* 登陆成功后,将cookie发给浏览器保存,以后登陆带上这个cookie,只要通过检查就可以免登陆
* 点击注销,会删除cookie
*/
http.rememberMe().rememberMeParameter("remember");
}
//定义认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// super.configure(auth);
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("shang").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2")
.and()
.withUser("yy").password(new BCryptPasswordEncoder().encode("111")).roles("VIP3");
}
}
最近下载更多
f22m1a2b2 LV17
1月23日
wanglinddad LV55
2024年5月26日
kk992127170 LV6
2022年10月25日
1234mama LV19
2022年6月7日
evagsd LV6
2022年5月30日
boydyoung LV1
2021年8月13日
lyd19931203 LV21
2021年6月1日
齐凌霄 LV7
2021年5月12日
shiopaaa LV13
2021年4月22日
huo_lang LV4
2020年12月4日

最近浏览