springsecurity--Config层代码常用代码指令

 前提搭建

使我们的类继承于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博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值