如何在Spring Boot [替换web.xml]中指定安全约束和安全角色?

时间:2018-12-23 01:45:37

标签: spring spring-boot weblogic

在某些情况下,我需要使用应用程序服务器(Weblogic)并实现安全性。我们正在将项目从春季转移到春季靴子 由于Spring Boot没有任何web.xml,我该如何通过Java代码编写等效的安全约束。

<security-constraint>
        <web-resource-collection>
            <web-resource-name>rest-application</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>xyz</role-name>
        </auth-constraint>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
</security-constraint>
<Security-role>
<role-name>xyz></role-name>
</security-role>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>abc</realm-name>
</login-config

Weblogic.xml将具有以下配置

<security-role-assignment>
<role-name>xyz</role-name>
<principal-name>pars</principal-name>
</security-role-assignment>

1 个答案:

答案 0 :(得分:0)

您可以使用Java config和基本身份验证来配置Spring Security。 确保添加spring安全依赖项。

替换您正在使用的用户/密码以及角色和密码算法。

@Configuration
@EnableWebSecurity
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
          .withUser("user1").password(passwordEncoder().encode("user1Pass"))
          .authorities("ROLE_USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
          .antMatchers("/public/paths").permitAll()
          .anyRequest().authenticated()
          .and()
          .httpBasic()

    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}