首页 > 其他分享 >设置不登录访问后端

设置不登录访问后端

时间:2023-04-03 17:39:03浏览次数:28  
标签:String 登录 HttpMethod permitAll antMatchers 访问 设置 get httpSecurity



  1. 取消多租户




  1. 定义的 Spring Security 配置适配器实现
@AutoConfiguration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class YudaoWebSecurityConfigurerAdapter {
 
    @Bean
    protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
        // 登出
        httpSecurity
                // 开启跨域
                .cors().and()
                // CSRF 禁用,因为不使用 Session
                .csrf().disable()
                // 基于 token 机制,所以不需要 Session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .headers().frameOptions().disable().and()
                // 一堆自定义的 Spring Security 处理器
                .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);
        // 登录、登录暂时不使用 Spring Security 的拓展点,主要考虑一方面拓展多用户、多种登录方式相对复杂,一方面用户的学习成本较高

        // 获得 @PermitAll 带来的 URL 列表,免登录
        Multimap<HttpMethod, String> permitAllUrls = getPermitAllUrlsFromAnnotations();
        // 设置每个请求的权限
        httpSecurity
                // ①:全局共享规则
                .authorizeRequests()
                // 1.1 静态资源,可匿名访问
                .antMatchers(HttpMethod.GET, "/*.html", "/**/*.html", "/**/*.css", "/**/*.js").permitAll()
                // 1.2 设置 @PermitAll 无需认证
                .antMatchers(HttpMethod.GET, permitAllUrls.get(HttpMethod.GET).toArray(new String[0])).permitAll()
                .antMatchers(HttpMethod.POST, permitAllUrls.get(HttpMethod.POST).toArray(new String[0])).permitAll()
                .antMatchers(HttpMethod.PUT, permitAllUrls.get(HttpMethod.PUT).toArray(new String[0])).permitAll()
                .antMatchers(HttpMethod.DELETE, permitAllUrls.get(HttpMethod.DELETE).toArray(new String[0])).permitAll()
                // 1.3 基于 yudao.security.permit-all-urls 无需认证
                .antMatchers(securityProperties.getPermitAllUrls().toArray(new String[0])).permitAll()
                // 1.4 设置 App API 无需认证
                .antMatchers(buildAppApi("/**")).permitAll()
                // 1.5 验证码captcha 允许匿名访问
                .antMatchers("/captcha/get", "/captcha/check","/express/accept").permitAll()
                // 1.6 webSocket 允许匿名访问
                .antMatchers("/websocket/message").permitAll()
                // ②:每个项目的自定义规则
                .and().authorizeRequests(registry -> // 下面,循环设置自定义规则
                        authorizeRequestsCustomizers.forEach(customizer -> customizer.customize(registry)))
                // ③:兜底规则,必须认证
                .authorizeRequests()
                .anyRequest().authenticated()
        ;

        // 添加 Token Filter
        httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        return httpSecurity.build();
    }
    
}





标签:String,登录,HttpMethod,permitAll,antMatchers,访问,设置,get,httpSecurity
From: https://blog.51cto.com/u_15840476/6166902

相关文章

  • Flask 和pythonweb框架介绍、flask快速使用、登录,显示用户信息小案例、配置文件方式、
    Flask和pythonweb框架介绍、flask快速使用、登录,显示用户信息小案例、配置文件方式、路由系统Flask和pythonweb框架介绍Flask和pythonweb框架的区别:Django框架: 大而全,内置的app很多,第三方的app很多Flask框架: 小而精,没有过多的内置app,只能完成web框架的基本功能,很多功能......
  • 使用Newtonsoft.Json在Net6中设置时间格式(含T/不含T)
    实例一:JsonSerializerSettingsjsonSettings=newJsonSerializerSettings{DateFormatString="yyyy-MM-ddHH:mm:ss.fff",};Modelmodel=newModel{CreatedAt=DateTime.UtcNow};......
  • Jmeter的http信息头管理器设置
    什么是信息头:请求头什么时候用?cookietoken或者是其他的信息的时候;我们的场景设计?反爬虫添加信息头--模拟浏览器去发送请求--user-agent  ......
  • .Net Core3.1 API访问进行频次限制
    首先,安装AspNetCore.RateLimitNuGet包。您可以通过NuGet包管理器控制台或VisualStudio的NuGet包管理器来执行此操作。安装后,您将在项目中看到一个名为AspNetCoreRateLimit的文件夹,其中包含中间件的配置类。接下来,您需要在Startup.cs文件中注册中间件。您可以在Configure......
  • macOS 运行xxx.command文件提示”无法执行,因为您没有正确的访问权限“解决方法
    使用苹果mac电脑运行.command文件时,是否遇到弹出”无法执行,因为您没有正确的访问权限“的窗口?遇到这种问题怎么解决呢?这里小编为大家带来了详细的解决方法,一起来看看吧!解决方法:方法一:打开终端工具,输入以下命令:sudosh注意后面有空格然后再把.command文件直接拖入终端按回车......
  • windows 10 系统 和 VMware Workstation 虚拟机网络互通设置
    windows10系统和VMwareWorkstation虚拟机网络互通设置 1,虚拟机设置网卡地址网关地址子网掩码2,VMwareWorkstation的编辑-虚拟网络编辑器,单击进入配置,为NAT类型。3,本地笔记本电脑的虚拟网卡配置地址网关掩码4,本地笔记本电脑使用secureCRT和winscp测试,连接和上传文件都OK......
  • 1 Flask 和pythonweb框架介绍、2 flask快速使用 、3 登录,显示用户信息小案例、4 配置
    目录1Flask和pythonweb框架介绍1.1flask介绍2flask快速使用3登录,显示用户信息小案例3.1login.html3.2home.html3.3detail.html3.4py文件4配置文件方式5路由系统5.1路由本质5.2路由参数add_url_rule5.3转换器1Flask和pythonweb框架介绍#pythonweb框架,本质都一......
  • Flask 和pythonweb框架介绍、flask快速使用、登录,显示用户信息小案例、配置文件方式、
    目录1Flask和pythonweb框架介绍1.1flask介绍2flask快速使用3登录,显示用户信息小案例3.1login.html3.2home.html3.3detail.html3.4py文件4配置文件方式5路由系统5.1路由本质5.2路由参数add_url_rule5.3转换器1Flask和pythonweb框架介绍#pythonweb框架,本质都一......
  • jupyter 主题设置
      参考:(63条消息)JupyterNotebook设置黑色背景主题,字体大小,代码自动补全_jupyter黑色背景_极客阿宝的博客-CSDN博客 pipinstalljupyterthemes-ihttps://mirrors.aliyun.com/pypi/simple jt-tmonokai-ffira-fs13-cellw90%-ofs11-dfs11-T-N......
  • 230123-Git命令行代理及加速设置
    ⭐️方法1:设置全局国内/国外代理gitconfig--globalhttp.proxyhttp://127.0.0.1:XXXXgitconfig--globalhttps.proxyhttp://127.0.0.1:XXXX⭐️方法2:仅设置github的代理gitconfig--globalhttp.https://github.com.proxyhttp://127.0.0.1:XXXXgitconfig--globalhttp......