package com.anhua.security.MySecurity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.SecurityBuilder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
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.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import javax.sql.DataSource;
import java.beans.Encoder;
@EnableWebSecurity
public class MySecurity extends WebSecurityConfigurerAdapter {
// 授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/111").hasRole("vip1")
.antMatchers("/112").hasRole("vip1")
.antMatchers("/222").hasRole("vip2")
.antMatchers("/221").hasRole("vip2");
http.formLogin();
http.logout();
}
// 认证
// 密码要进行编码
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("anhua")
.password(new BCryptPasswordEncoder().encode("123")).roles("vip1")
.and()
.withUser("anhua2").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip1");
}
}
spring-security的使用
最新推荐文章于 2024-02-01 23:33:15 发布