关于spring 中授权的使用,记录下,以便后期使用。
import javax.sql.DataSource;
import java.beans.Encoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home").hasRole("USER")
.and().formLogin();
}
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth
.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())
.withUser("jxm")
.password(new BCryptPasswordEncoder().encode("1"))
.roles("USER");
}
}
spring 保护授权
最新推荐文章于 2021-09-18 02:49:57 发布
本文介绍了一个使用Spring Security进行授权配置的例子。通过配置类SecurityConfig实现了基于角色的访问控制,只允许具有USER角色的用户访问特定路径。同时,还演示了如何使用BCryptPasswordEncoder来加密用户密码。
1512

被折叠的 条评论
为什么被折叠?



