首先定义一个SecurityConfig.java类继承WebSecurityConfigurerAdapter

重写方法

@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        //1.配置认证,开启formLogin模式;2.配置权限;3.禁用跨站csrf攻击防御。
        httpSecurity
                .formLogin()//开启formLogin模式
                    .loginPage("/login.html")//用户未登录时,访问任何资源都跳转到此路径
                    .loginProcessingUrl("/login")//登陆表单form中action的地址,处理认证请求的路径
                    .usernameParameter("username")//默认是username
                    .passwordParameter("password")//默认是password
                    .defaultSuccessUrl("/index")//登陆成功后跳转的接口
                    .failureUrl("/login.html")//登陆失败跳转的页面
                    .and()//使用and连接
                .authorizeRequests()//配置权限
                    .antMatchers("/login.html","/login")
                    .permitAll()//用户都可以访问
                    .antMatchers("/order")//需要对外暴露的资源路径
                    .hasAnyAuthority("ROLE_user", "ROLE_admin")//user角色和admin角色可以访问
                    .antMatchers("/system.user","/system.role","/system/menu")
                    .hasAnyRole("admin")//admin角色可以访问
                    //除上面所有所有的请求都需要认证
                    .anyRequest().authenticated()//authenticated()要求在执行该请求的时候,必须已经登陆
                    .and()
                .csrf().disable();//禁用跨站csrf攻击防御,否则无法登陆
        httpSecurity.logout().logoutUrl("/logout");//注销功能
    }

权限相关方法:
使用内存验证用户

@Override
   protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.inMemoryAuthentication()
               .withUser("user")
               .password(bCryptPasswordEncoder().encode("1234"))
               .roles("user")
               .and()
               .withUser("admin")
               .password(bCryptPasswordEncoder().encode("1234"))
               .roles("admin")
               .and()
               .passwordEncoder(bCryptPasswordEncoder());
   }

引自此