栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java配置和Spring Security 3.2的安全方法注释

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java配置和Spring Security 3.2的安全方法注释

当您使用

protectedregisterAuthentication
的方法
WebSecurityConfigurerAdapter
是范围界定身份验证到
WebSecurityConfigurerAdapter
如此
EnableGlobalMethodSecurity
无法找到它。如果您考虑一下……这是有道理的,因为该方法受到保护。

您看到的错误实际上是一个调试语句(请注意级别为DEBUG)。原因是Spring Security将尝试几种不同的方法来自动连接Global Method
Security。具体来说,

EnableGlobalMethodSecurity
将尝试以下方法来尝试获取
AuthenticationManager

  • 如果您扩展
    GlobalMethodSecurityConfiguration
    并覆盖,
    registerAuthentication
    它将使用
    AuthenticationManagerBuilder
    传入的。这允许
    AuthenticationManager
    您以与
    WebSecurityConfigurerAdapter
  • 尝试从的全局共享实例进行构建
    AuthenticationManagerBuilder
    ,如果失败,它将记录您所看到的错误消息(请注意,日志还指出“目前还可以,我们将尝试直接使用AuthenticationManager”)
  • 尝试使用
    AuthenticationManager
    暴露为Bean的。

对于您的代码,最好使用以下代码:

@Configuration@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled=true)public class MultiSecurityConfig {    // Since MultiSecurityConfig does not extend GlobalMethodSecurityConfiguration and    // define an AuthenticationManager, it will try using the globally defined    // AuthenticationManagerBuilder to create one    // The @Enable*Security annotations create a global AuthenticationManagerBuilder     // that can optionally be used for creating an AuthenticationManager that is shared    // The key to using it is to use the @Autowired annotation    @Autowired    public void registerSharedAuthentication(AuthenticationManagerBuilder auth) throws Exception {        auth .inMemoryAuthentication()     .withUser("user").password("password").roles("USER").and()     .withUser("admin").password("password").roles("USER", "ADMIN");    }    @Configuration    @Order(1)    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {        // Since we didn't specify an AuthenticationManager for this class,        // the global instance is used        protected void configure(HttpSecurity http) throws Exception { http     .antMatcher("/api/**")     .httpBasic();        }    }    @Configuration    public static class FormWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {        // Since we didn't specify an AuthenticationManager for this class,        // the global instance is used        public void configure(WebSecurity web) throws Exception { web     .ignoring()         .antMatchers("/static/**","/status");        }        protected void configure(HttpSecurity http) throws Exception { http     .authorizeRequests()         .anyRequest().hasRole("USER")         .and()     .formLogin()         .loginPage("/login")         .permitAll();        }    }}

注意:有关此问题的更多文档将在未来几天内添加到参考中。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508569.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号