Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Spring Security Java配置中创建多个HTTP节_Java_Spring Security_Config - Fatal编程技术网

在Spring Security Java配置中创建多个HTTP节

在Spring Security Java配置中创建多个HTTP节,java,spring-security,config,Java,Spring Security,Config,使用SpringSecurityXML配置,您可以定义多个HTTP元素,为应用程序的不同部分指定不同的访问规则。中给出的示例分别定义了应用程序的有状态和无状态部分,前者使用会话和表单登录,后者不使用会话和基本身份验证: <!-- Stateless RESTful service using Basic authentication --> <http pattern="/restful/**" create-session="stateless"> <in

使用SpringSecurityXML配置,您可以定义多个HTTP元素,为应用程序的不同部分指定不同的访问规则。中给出的示例分别定义了应用程序的有状态和无状态部分,前者使用会话和表单登录,后者不使用会话和基本身份验证:

<!-- Stateless RESTful service using Basic authentication -->
<http pattern="/restful/**" create-session="stateless">
    <intercept-url pattern='/**' access='ROLE_REMOTE' />
    <http-basic />
</http>

<!-- Empty filter chain for the login page -->
<http pattern="/login.htm*" security="none"/>

<!-- Additional filter chain for normal users, matching all other requests -->
<http>
    <intercept-url pattern='/**' access='ROLE_USER' />
    <form-login login-page='/login.htm' default-target-url="/home.htm"/>
    <logout />
</http>

使用此功能,我可以为我的web服务禁用CSRF保护。但我确实需要一个完整的单独HTTP配置,这样我就可以禁用会话并指定不同的入口点。我知道我可以使用
requestMatcher
requestMatchers
来限制它所适用的URI,但您似乎不能使用它来创建单独的配置。就像我需要两个
configure(HttpSecurity-security)
方法一样。

在Spring security中,模拟Java配置中XML中多个
元素的行为,为安全配置创建多个类。通常,为
HttpSecurity
的安全定义创建具有多个内部类的公共安全配置是最好/最容易的。请参阅以获取示例

这里是Spring官方安全文档中的相关部分:

为安全配置创建多个类。最简单的方法是使用一些内部
@configuration
类创建一个通用配置,用于配置
HttpSecurity
。谢谢。这就是答案。此外,还显示了一个使用静态内部类执行此操作的示例。你能把你的评论放在一个答案中,这样我就可以把它标记为答案并给它投票吗?文档的URL引用(
8.6高级命名空间配置
)被破坏了。不是你可以在这里找到它github链接现在也断了。我在这里遵循了这个示例,但是由于第二个配置是catch all,所有的URL都被第二个配置截获。只要我在第二个httpsecurity中输入一个特定的url(通过antMatcher),所有的url都可以在没有身份验证的情况下访问。这个答案的关键是每个安全配置类必须在第一个antMatcher()中标识一个唯一的url路径。例如:
protectedvoidconfigure(HttpSecurity-http){http.antMatcher(“/secure**”)
旁边:
protectedvoidconfigure(HttpSecurity-http){http.antMatcher(/api/**”)
问题中的代码的问题是此代码匹配所有URL:
security.authorizedRequests()
@informatik01当我单击“此处”时,我得到404。不确定原因是什么is@SandeepShukla采样的tp已在GitHub中移动到另一个位置。我已更新链接以指向当前位置,它现在可用。
@Override
public void configure(WebSecurity security)
{
    security.ignoring().antMatchers("/resource/**", "/favicon.ico");
}

@Override
protected void configure(HttpSecurity security) throws Exception
{
    security
            .authorizeRequests()
                .anyRequest().authenticated()
            .and().formLogin()
                .loginPage("/login").failureUrl("/login?loginFailed")
                .defaultSuccessUrl("/ticket/list")
                .usernameParameter("username")
                .passwordParameter("password")
                .permitAll()
            .and().logout()
                .logoutUrl("/logout").logoutSuccessUrl("/login?loggedOut")
                .invalidateHttpSession(true).deleteCookies("JSESSIONID")
                .permitAll()
            .and().sessionManagement()
                .sessionFixation().changeSessionId()
                .maximumSessions(1).maxSessionsPreventsLogin(true)
                .sessionRegistry(this.sessionRegistryImpl())
            .and().and().csrf()
                .requireCsrfProtectionMatcher((r) -> {
                    String m = r.getMethod();
                    return !r.getServletPath().startsWith("/services/") &&
                            ("POST".equals(m) || "PUT".equals(m) ||
                                    "DELETE".equals(m) || "PATCH".equals(m));
                });
}