【问题标题】:Going from web.xml to Spring Boot从 web.xml 到 Spring Boot
【发布时间】:2017-09-15 18:55:02
【问题描述】:

我正在升级现有应用程序以使用 Spring Boot 并远离 web.xml。 虽然我已经实现了功能,但我的 web.xml 中有一部分在我的一生中无法弄清楚如何升级到 Spring Boot。 (如果重要的话,我使用的是 1.5.6)

我的 web.xml 的相关部分:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>basic</web-resource-name>
        <url-pattern>*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>User</role-name>
    </auth-constraint>
</security-constraint>

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>myrealm</realm-name>
</login-config>

这是我的应用程序类:

@SpringBootApplication
public class Application extends SpringBootServletInitializer{

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
         return builder.sources(Application.class);
    }
}

我无法确定如何使用基于代码的方法设置任何东西来反映安全约束或登录配置的功能。

如果有人对如何或可以为我指明正确的方向有任何想法,我将不胜感激。几个小时的谷歌搜索和尝试解决方案让我一无所获。

【问题讨论】:

  • 也许找教程“spring boot 安全教程”?通过这个搜索词,您可以找到很多。

标签: spring spring-boot weblogic web.xml weblogic12c


【解决方案1】:

查看@EnableWebSecurity 和WebSecurityConfigurerAdapter

请看下面的例子:

@EnableWebSecurity
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/**").authenticated()
            .anyRequest()
            .permitAll()
            .and().httpBasic()
            .and().sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2015-04-18
    • 2014-04-18
    • 2019-10-06
    • 1970-01-01
    • 2013-10-03
    • 2016-03-09
    相关资源
    最近更新 更多