Loading
0

Spring Security 权限控制

重要配置类:

org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter

配置指定用户可访问:

package cn.com.joycode.nimble.demo;

import org.springframework.context.annotation.Configuration;
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.builders.WebSecurity;
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;

@EnableWebSecurity
@Configuration
public class WebSecuritConfig extends WebSecurityConfigurerAdapter {
 
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .passwordEncoder(new BCryptPasswordEncoder())
                .withUser("joycode")
                .password("$2a$10$.xvLEE4tZckoiYiBta8n5OQ/TjIGL7APXzKQKYKESqVluLiUsgKHC")
                .roles("user");
    }
}

上面的配置代码指定了用户"joycode"可以访问,密码也为"joycode",使用自带的BCrypt加密算法加密过。

参考:

视频:

最后编辑于:2021/7/18作者: 编程小战

我不入地狱,谁入地狱?

评论已关闭