Spring Boot + 安全 + 多 HTTP Web 配置

2024-04-27

我正在尝试使用 spring-boot 和 spring security 来做一个示例。我的想法是创建一个网络应用程序并提供一个API,我希望两者都有安全性;所以我需要创建一个多 http Web 安全配置,但它不起作用。

我点击了这个链接http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#multiple-httpsecurity http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#multiple-httpsecurity但没有成功。而且,我收到这个错误

创建名为“webSecurityConfiguration”的 bean 时出错:自动装配依赖项注入失败;嵌套异常是 java.lang.IllegalStateException:无法将 org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer 应用于已构建的对象

我正在使用的配置如下:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableGlobalAuthentication
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfiguration { 

@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth
        .inMemoryAuthentication()
            .withUser("user").password("12345").roles("USER").and()
            .withUser("admin").password("12345").roles("USER", "ADMIN");
}

@Configuration
@Order(1)
public static class ApiConfigurationAdapter extends
        WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/api/**")
            .authorizeRequests()
                .anyRequest().hasRole("ADMIN")
                .and()
            .httpBasic();
    }
}

@Configuration
@Order(2)
public static class WebConfigurationAdapter extends
        WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
            .ignoring()
                .antMatchers("/resources/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()                    
                .antMatchers("/", "/home").permitAll()
            .anyRequest()
                .authenticated()
            .and()
                .formLogin()
                    .loginPage("/login").permitAll()
            .and()
                .logout().permitAll();
    }
    }
}

提前致谢


经过大量阅读后,我发现了一些对我有用的东西:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {

    @Resource(name = "customUserDetailsService")
    protected CustomUserDetailsService customUserDetailsService;

    @Resource
    private DataSource dataSource;

    @Autowired
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserDetailsService);
    }

    @Configuration
    @Order(1)
    public static class ApiConfigurationAdapter extends WebSecurityConfigurerAdapter {
        @Resource(name = "restUnauthorizedEntryPoint")
        private RestUnauthorizedEntryPoint restUnauthorizedEntryPoint;
        @Resource(name = "restAccessDeniedHandler")
        private RestAccessDeniedHandler restAccessDeniedHandler;

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            SecurityConfigurer<DefaultSecurityFilterChain, HttpSecurity> securityXAuthConfigurerAdapter = new XAuthTokenConfigurer(
                    userDetailsServiceBean());

            // @formatter:off
            http
                .antMatcher("/api/**").csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .exceptionHandling()
                    .authenticationEntryPoint(restUnauthorizedEntryPoint)
                    .accessDeniedHandler(restAccessDeniedHandler)
                .and()
                    .authorizeRequests()
                        .antMatchers(HttpMethod.POST, "/api/authenticate").permitAll()
                        .anyRequest().hasRole("ADMIN")
                        .and()
                        .apply(securityXAuthConfigurerAdapter);
            // @formatter:on
        }
    }

    @Configuration
    @Order(2)
    public static class WebConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // @formatter:off
            http
                .authorizeRequests()
                    .antMatchers("/", "/home").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                        .loginPage("/login").permitAll()
                    .and()
                    .logout().permitAll()
            ;
            // @formatter:on
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Boot + 安全 + 多 HTTP Web 配置 的相关文章

随机推荐

  • PHP 中“

    默认情况下已启用从5 4 0开始 https www php net ChangeLog 5 php 5 4 0不管php ini设置
  • 如何编写虚拟机[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • c.JSON gin.H{()} 输出空对象

    我刚刚开始学习 GO lang 结合 Gin 框架 我决定编写一些简单的 api 来获取有关酒精饮料的数据 我当前的问题是 api get 方法http localhost 8080 alcohol drinks 返回空数据对象 My co
  • 不使用 PIN 的 Twitter 身份验证

    我正在尝试验证 Windows Phone 中的用户帐户 我找到了这个 C 库来完成这项工作 tweetsharp 他们的示例非常清楚 但他们使用 pin 码来验证用户身份 using TweetSharp Pass your creden
  • 从 Nipype docker 镜像 CommandNotFound 构建奇点配方

    我有以下奇点容器配方 bin bash Bootstrap docker From nipype nipype latest labels Version v1 0 post Install nano apt get update apt
  • 将不同的内容添加到 flutter moor 查询中

    我有以下颤动沼泽查询 select recipeGarnishes where tbl gt tbl postGarnish equals true get 我该如何添加distinct查询条件 更新 我想写的查询是 select DIST
  • Maven编译失败(但Eclipse下编译成功)

    在构建我的网络项目时Eclipse 一切安好 没有错误 没有警告 然而 在构建项目时Maven it failes 下面是输出形式mvn compile c Users jwa Desktop tets traffic web gt mvn
  • Azure 有害队列计数警报规则

    在之前的一个项目中 我设法设置了一个警报规则 该规则会查看有害队列消息计数 并在队列中存在某些内容时 每天一次 使用 webhook 向 slack 发出警报 我试图找到它在 Azure 中的位置 因为看起来事情已经发生了变化 如果这不是
  • 在php中生成随机字符串作为文件名[重复]

    这个问题在这里已经有答案了 我将如何创建与文件名一起使用的随机文本字符串 我正在上传照片并在完成后重命名它们 所有照片都将存储在一个目录中 因此它们的文件名必须是唯一的 有这样做的标准方法吗 有没有办法在尝试覆盖之前检查文件名是否已经存在
  • 将字符串转换为个位数并求和

    我花了几个小时尝试寻找解决方案来完成我认为很简单的任务 但我失败了 我有一个由 3 个不同字符组成的字符串 I R O 长度从 1 到 6 E g IRRROO RRORRR IIR RIRRO 每个字符代表一个数字I 1 R 2 O 3我
  • 什么是 NullPointerException,如何修复它?

    这个问题的答案是社区努力 help privileges edit community wiki 编辑现有答案以改进这篇文章 目前不接受新的答案或互动 什么是空指针异常 java lang NullPointerException 以及是什
  • Prolog 匹配 vs miniKanren 统一

    在 Prolog 人工智能编程中 Bratko 在第 58 页说了以下内容 Prolog 中的匹配对应于逻辑中所谓的统一 但是 我们避免使用 统一 这个词 因为出于效率原因 在大多数 Prolog 系统中 匹配的实现方式并不完全对应于统一
  • onDetach 当片段时调用

    我在方向改变后显示相同的片段时遇到问题 我将其放入后台并弹出 它跳转到onCreateView等 但随后它调用onDetach 这导致显示错误的片段 代码如下 fragment public View onCreateView Layout
  • 如何将 JEditorPane 插入 JTable 单元格?

    我想将 JEditorPane 放入 JTable 单元格中 我写过这个 jTabel1 setDefaultRenderer String class new StringEditorPane class StringEditorPane
  • NaN 是关联容器的有效键值吗?

    考虑 C 中的有序和无序关联容器double Is NaN有效的密钥类型 对于有序容器 我应该说 不 因为它不尊重严格的弱排序 对于无序的容器 我不知道 以下是 GCC 4 6 2 中发生的情况 include
  • 正则表达式匹配不在数组中的逗号(用方括号括起来)

    我有一个对象 想将其表示为带有一些附加格式的字符串 这是我的代码 stringify object and remove double quotations let outPut JSON stringify myObject replac
  • 执行 grails/groovy 时,Linux 上没有可用的控制台输出

    当执行 groovy 脚本或 grails 应用程序时 没有可用的输出 输入 gt 只有一个清晰的控制台屏幕 即使不启动 X Window System 输出也是不可见的 我也尝试过 grailscompile plain output 也
  • 如何在不使用 SPLITSHARD 的情况下动态向 SolrCloud 添加节点?

    我已经设置了Solr云有 4 个碎片 我向 SolrCloud 添加了 8 个节点 4 个领导者和 4 个副本 每个节点运行在不同的机器上 但后来我发现我的数据越来越多 每天400万文件 这样我的 4 个分片就不够用了 因此 我想动态地向该
  • Python-docx:是否可以在特定位置(而不是末尾)向段落添加新的运行

    我想为 MS Word 文本中更正的单词设置样式 由于无法更改运行中的文本样式 因此我想在现有段落中插入具有新样式的新运行 for p in document paragraphs for run in p runs if text in
  • Spring Boot + 安全 + 多 HTTP Web 配置

    我正在尝试使用 spring boot 和 spring security 来做一个示例 我的想法是创建一个网络应用程序并提供一个API 我希望两者都有安全性 所以我需要创建一个多 http Web 安全配置 但它不起作用 我点击了这个链接