- Hands-On Spring Security 5 for Reactive Applications
- Tomcy John
- 181字
- 2021-07-23 18:59:22
Step 1—Spring Security configuration setup
We will now create the all-important Spring Security configuration class and make sure that the default filter chain for Spring Security is set up to secure all the resources:
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username, password, enabled"
+ " from users where username = ?")
.authoritiesByUsernameQuery("select username, authority "
+ "from authorities where username = ?")
.passwordEncoder(new BCryptPasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().hasAnyRole("ADMIN", "USER")
.and()
.httpBasic(); // Use Basic authentication
}
}
In Spring Security configuration, the first thing that we do is tell Spring Security that you will have to authenticate the user against a database by using a defined user query and checking the user's authority using the defined authority query.
We then set up the authentication mechanism to retrieve the user's credentials. Here we are using basic authentication as the mechanism to capture user credentials. Please note that the role names being used to check doesn't have the prefix ROLE_.
推薦閱讀
- 網(wǎng)絡(luò)安全與管理
- 白帽子講Web安全(紀(jì)念版)
- 網(wǎng)絡(luò)安全應(yīng)急管理與技術(shù)實(shí)踐
- 信息系統(tǒng)安全檢測(cè)與風(fēng)險(xiǎn)評(píng)估
- Enterprise Cloud Security and Governance
- 數(shù)字安全藍(lán)皮書(shū):本質(zhì)屬性與重要特征
- Computer Forensics with FTK
- 硬黑客:智能硬件生死之戰(zhàn)
- 計(jì)算機(jī)網(wǎng)絡(luò)安全技術(shù)研究
- 可信計(jì)算3.0工程初步(第二版)
- 云原生安全技術(shù)實(shí)踐指南
- Advanced Penetration Testing for Highly:Secured Environments(Second Edition)
- Kerberos域網(wǎng)絡(luò)安全從入門(mén)到精通
- Web安全之深度學(xué)習(xí)實(shí)戰(zhàn)
- 安全防御入門(mén)手冊(cè)