Java 用于RESTFul的Spring Security HTTP Basic和用于web注释的FormLogin(Cookies)

Java 用于RESTFul的Spring Security HTTP Basic和用于web注释的FormLogin(Cookies),java,spring,spring-mvc,spring-security,spring-boot,Java,Spring,Spring Mvc,Spring Security,Spring Boot,具体的 我只想对特定的URL模式使用HTTP基本身份验证 详细信息 我正在为我的应用程序创建一个API接口,它需要通过简单的HTTP基本身份验证进行身份验证。但是其他网页不应该使用HTTP basic,而应该使用正常形式的登录 当前配置-不工作 @Override protected void configure(HttpSecurity http) throws Exception { http //HTTP Security .csrf().disable()

具体的

我只想对特定的URL模式使用HTTP基本身份验证

详细信息

我正在为我的应用程序创建一个API接口,它需要通过简单的HTTP基本身份验证进行身份验证。但是其他网页不应该使用HTTP basic,而应该使用正常形式的登录

当前配置-不工作

@Override
protected void configure(HttpSecurity http) throws Exception {
    http //HTTP Security
            .csrf().disable() //Disable CSRF
            .authorizeRequests() //Authorize Request Configuration
                .antMatchers("/connect/**").permitAll()
                .antMatchers("/", "/register").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/api/**").hasRole("API")
                .anyRequest().authenticated()
            .and() //HTTP basic Authentication only for API
                .antMatcher("/api/**").httpBasic()
           .and() //Login Form configuration for all others
                .formLogin().loginPage("/login").permitAll()
            .and() //Logout Form configuration
                .logout().permitAll();

}

等了两天,没有得到任何帮助。但我的研究为我提供了一个解决方案:)

解决方案

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private AuthenticationProvider authenticationProvider;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
    }

    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                    .antMatcher("/api/**")
                    .authorizeRequests()
                        .anyRequest().hasAnyRole("ADMIN", "API")
                        .and()
                    .httpBasic();
        }
    }

    @Configuration
    @Order(2)
    public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/css/**", "/js/**", "/img/**", "/lib/**");
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable() //HTTP with Disable CSRF
                    .authorizeRequests() //Authorize Request Configuration
                        .antMatchers("/connect/**").permitAll()
                        .antMatchers("/", "/register").permitAll()
                        .antMatchers("/admin/**").hasRole("ADMIN")
                        .anyRequest().authenticated()
                        .and() //Login Form configuration for all others
                    .formLogin()
                        .loginPage("/login").permitAll()
                        .and() //Logout Form configuration
                    .logout().permitAll();
        }
    }
}

我不知道这是否有帮助,但我无法实现上述解决方案。我找到了一个定义单一安全性的解决方法

@配置类

延伸

Web安全配置适配器

配置了httpBasic()和formLogin()。然后我创建了一个自定义

CustomAuthEntryPoint实现AuthenticationEntryPoint

在开始方法中具有以下逻辑:

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException
   {
        String urlContext = UtilityClass.extractUrlContext(request);
        if (!urlContext.equals(API_URL_PREFIX))
        {
            String redirectUrl = "urlOfFormLogin"
            response.sendRedirect(request.getContextPath() + redirectUrl);
       }
        else
        {
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        }

不知道这是关于这个问题的“最佳实践策略”

如何删除基本的http验证弹出窗口。我尝试了所有方法,但都无法做到。请不要使用httpBasic,请使用formLogin。尝试了之后,没有成功,我使用httpBasic()禁用了它。禁用()并使用formLogin(),但似乎无法修复。请在单独的问题中发布您的代码,并放置链接。别忘了把你的配置源代码放进去。为什么要使用@Order注释?