前提搭建

使我们的类继承于WebSecurityConfigurerAdapter这个类
同时调用service还有新建一个bean方法
 
 @Bean
public PasswordEncoder getPassword() {
    return new BCryptPasswordEncoder();
} 
 
这个代码是可以自定义账户和密码

自定义登录账户和密码写在service类中
下方的红框中是表名要给授权的"角色或者身份"

同时config这边重写方法
重写类中的方法
这行代码意思是获取登录时的账号和密码
auth.userDetailsService(secourityService).passwordEncoder(getPassword()); 
接下来在重写方法
之后在方法里面写入需求指令
代码指令
http.cors();//允许跨域访问
 
 http.formLogin()//告诉security 使用自定义登录页面
        .loginPage("/login.html")//告诉security,页面在哪里
        .loginProcessingUrl("/dologin")//告诉表单要提交的地址
        //注册登录处理类
        .successHandler(new LoginSuccessHandler())//登录成功
        .failureHandler(new LoginFildHandler())//登录失败
        .permitAll();//不管登入,不登入 都能访问 
  
 
 
 /**
 * 未登录,就像访问系统资源
 */
http.exceptionHandling()
        .authenticationEntryPoint(new MyAuthenticationEntryPoint())//未登录,访问被拦截
        .accessDeniedHandler(new MyAccessDeniedHandler());//登录上了,但是没有对应的操作权限 
  
 
/** * 退出系统 */ http.logout().logoutSuccessHandler(new MyLogoutSuccessHandler());//退出系统
http.authorizeRequests()//配置请求权限的.anyRequest().authenticated();//所有请求都拦截
http.csrf().disable(); //关闭跨站脚本攻击
 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors();//允许跨域访问
        http.formLogin()//告诉security 使用自定义登录页面
                .loginPage("/login.html")//告诉security,页面在哪里
                .loginProcessingUrl("/dologin")//告诉表单要提交的地址
                //注册登录处理类
                .successHandler(new LoginSuccessHandler())//登录成功
                .failureHandler(new LoginFildHandler())//登录失败
                .permitAll();//不管登入,不登入 都能访问
        /**
         * 未登录,就像访问系统资源
         */
        http.exceptionHandling()
                .authenticationEntryPoint(new MyAuthenticationEntryPoint())//未登录,访问被拦截
                .accessDeniedHandler(new MyAccessDeniedHandler());//登录上了,但是没有对应的操作权限
        /**
         * 退出系统
         */
        http.logout().logoutSuccessHandler(new MyLogoutSuccessHandler());//退出系统
        http.authorizeRequests() //配置请求权限的
                .anyRequest().authenticated();//所有请求都拦截
        http.csrf().disable(); //关闭跨站脚本攻击
    } 
一般常用登录的我们都只有这五个指令,其他的可自行拓展学习
补充
permitAll()
Spring Security中permitAll()和anonymous()的区别_magic_818的博客-CSDN博客
bean
Spring中Bean注解含义_magic_818的博客-CSDN博客



















