11、springsecurity

  • @GetMapping
    用于将HTTP GET请求映射到特定处理程序方法的注释。具体来说,@GetMapping是一个作为快捷方式的组合注释
    @RequestMapping(method = RequestMethod.GET)。

  • @PostMapping
    用于将HTTP POST请求映射到特定处理程序方法的注释。具体来说,@PostMapping是一个作为快捷方式的组合注释@RequestMapping(method = RequestMethod.POST)。

  • @RequestMapping:
    一般情况下都是用@RequestMapping(method=RequestMethod.),因为@RequestMapping可以直接替代以上两个注解,但是以上两个注解并不能替代@RequestMapping,@RequestMapping相当于以上两个注解的父类!

代码缩进

向后缩进:选择好代码,按【TAB】

向前缩进:选择好代码,按【shift】+【TAB】

规范缩进:全选代码,按【Ctrl】 + 【Alt 】+【 i 】

代码

点击查看代码
@EnableWebSecurity
public class SecurityConfig 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");
        // 没有权限默认跳到登录页面
        http.formLogin();
    }
    // 认证

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 这些正常在数据库里面写
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("root").password("123456").roles("vip1","vip2","vip3")
                .and()
                .withUser("guest").password("123456").roles("vip1");
    }
}
   
posted @ 2022-09-25 02:21  天井听雨  阅读(17)  评论(0编辑  收藏  举报