【问题标题】:Java configuration for security constraints in a Spring REST applicationSpring REST 应用程序中安全约束的 Java 配置
【发布时间】:2016-11-09 05:11:39
【问题描述】:

我正在使用 Spring REST(没有 web.xml)构建一个应用程序。 REST 调用工作正常,但我需要添加一些易于通过 web.xml 添加的安全约束,但由于我使用的是没有 web.xml 的 Spring 4,所以我需要帮助通过 Java 配置添加 web.xml 部分。

我的web.xml

<security-role>
     <role-name>all</role-name>
</security-role>
<security-constraint>
    <web-resource-collection>
         <web-resource-name>test</web-resource-name>
         <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
         <role-name>all</role-name>
    </auth-constraint>
</security-constraint>


我需要通过 Java 配置来配置这个 web.xml 的帮助。可能这可以通过 Spring Security 添加,但不确定如何添加。

【问题讨论】:

    标签: java spring spring-mvc spring-security configuration


    【解决方案1】:

    这是您如何使用@Configuration 使用自定义约束实现安全性并覆盖 WebSecurityConfigurerAdapter 类的配置方法。

     @Configuration
        public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
    
            @Autowired
            DataSource datasource;
            Logger logger = LoggerFactory.getLogger(getClass());
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
    
                http.httpBasic().and().authorizeRequests().antMatchers("/public/**")
                        .permitAll().antMatchers("/admin/**").hasAuthority("admin")
                        .antMatchers("/user/**").hasAuthority("user")
                        .and()
                        .logout()
                        // Logout requires form submit. Bypassing the same.
                        .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                        .logoutSuccessUrl("/index.html").and()
                        .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
                        .csrf().disable();
    
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-02
      • 1970-01-01
      • 2014-07-07
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      相关资源
      最近更新 更多