【问题标题】:How to specify security constrains and security roles in spring boot [replacement of web.xml]?spring boot【替换web.xml】中如何指定安全约束和安全角色?
【发布时间】:2018-12-23 01:45:36
【问题描述】:

我有一个需要使用应用程序服务器 (Weblogic) 并实现安全性的案例。我们正在将项目从 spring 转移到 spring boot 由于 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>

【问题讨论】:

标签: spring spring-boot weblogic


【解决方案1】:

您可以使用带有基本身份验证的 Java 配置来配置 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2012-11-23
    • 2023-03-30
    • 2012-04-08
    • 1970-01-01
    相关资源
    最近更新 更多