【问题标题】:Spring Security, secured and none secured accessSpring Security,安全和非安全访问
【发布时间】:2016-12-27 09:45:39
【问题描述】:

我正在做一个需要先登录的小应用程序。但是对于一些 3rd 方工具,我想提供一个不需要登录的 API。登录本身工作正常,API 本身工作,但我不知道如何告诉 Spring Security,无需身份验证即可访问 API。我在这里和其他网站上检查了几个主题并尝试了不同的版本,但没有一个有效。每次我尝试访问 API 时,我都会被转发到登录表单并且必须先登录。

到目前为止,我的代码在我的 Spring Security 配置中看起来像这样:

/**
 * configuration of spring security, defining access to the website
 * 
 * @param http
 * @throws Exception 
 */
@Override
protected void configure(HttpSecurity http) throws Exception {        
    http.authorizeRequests()                
            .antMatchers("/rest/open**").permitAll()
            .antMatchers("/login**").permitAll()
            .and()
        .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .failureUrl("/login?error")
            .defaultSuccessUrl("/dashboard")
            .loginProcessingUrl("/j_spring_security_check")
            .usernameParameter("username")
            .passwordParameter("password")
            .and()
        .logout()
            .logoutUrl("/j_spring_security_logout")
            .logoutSuccessUrl("/login?logout")
            .and()
        .csrf();
}

还有我的控制器:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PredictionOpenRestController {

    @RequestMapping("/rest/open/prediction")
    public String getPrediction() {
        return "First Try!";
    }
}

不知何故,我不得不感觉错过了一些东西。

【问题讨论】:

    标签: java spring security spring-security


    【解决方案1】:

    Spring Security Reference:

    我们的示例只要求对用户进行身份验证,并且对我们应用程序中的每个 URL 都进行了验证。我们可以通过向我们的http.authorizeRequests() 方法添加多个孩子来为我们的 URL 指定自定义要求。例如:

    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()                                                                
                .antMatchers("/resources/**", "/signup", "/about").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")  
                .anyRequest().authenticated()
                .and()
            // ...
            .formLogin();
    }
    

    1 http.authorizeRequests() 方法有多个子级,每个匹配器都按照声明的顺序进行考虑。

    2 我们指定了任何用户都可以访问的多个 URL 模式。具体来说,如果 URL 以“/resources/”开头、等于“/signup”或等于“/about”,则任何用户都可以访问请求。

    3 任何以“/admin/”开头的 URL 都将被限制为具有角色“ROLE_ADMIN”的用户。您会注意到,由于我们正在调用 hasRole 方法,因此我们不需要指定“ROLE_”前缀。

    4 任何以“/db/”开头的 URL 都要求用户同时拥有“ROLE_ADMIN”和“ROLE_DBA”。您会注意到,由于我们使用的是 hasRole 表达式,因此我们不需要指定“ROLE_”前缀。

    5 任何尚未匹配的 URL 只需要对用户进行身份验证

    您对.authorizeRequests() 的第二次使用会覆盖第一次。

    另见AntPathMatcher:

    映射使用以下规则匹配 URL:

    ? 匹配一个字符

    * 匹配零个或多个字符

    ** 匹配路径中的零个或多个目录

    例子

    com/t?st.jsp — 匹配 com/test.jsp 但也匹配 com/tast.jspcom/txst.jsp

    com/*.jsp — 匹配 com 目录中的所有 .jsp 文件

    com/**/test.jsp — 匹配 com 路径下的所有 test.jsp 文件

    org/springframework/**/*.jsp — 匹配org/springframework 路径下的所有.jsp 文件

    org/**/servlet/bla.jsp — 匹配 org/springframework/servlet/bla.jsp 但也匹配 org/springframework/testing/servlet/bla.jsporg/servlet/bla.jsp

    您修改后的代码:

    protected void configure(HttpSecurity http) throws Exception {        
        http.authorizeRequests()                
                .antMatchers("/rest/open/**").permitAll()
                .antMatchers("/login/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error")
                .defaultSuccessUrl("/dashboard")
                .loginProcessingUrl("/j_spring_security_check")
                .usernameParameter("username")
                .passwordParameter("password")
                .and()
            .logout()
                .logoutUrl("/j_spring_security_logout")
                .logoutSuccessUrl("/login?logout")
                .and()
            .csrf();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 2020-02-21
      • 2019-06-25
      相关资源
      最近更新 更多