当前位置: 首页 > 面试题库 >

用于Web的RESTFul和FormLogin(Cookies)的Spring Security HTTP Basic-批注

许波涛
2023-03-14
问题内容

具体而言

我只想对特定的网址格式进行HTTP基本身份验证。

详细地

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

当前配置-不起作用

@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();

}

问题答案:

等待了2天,在这里没有任何帮助。但是我的研究为我提供了解决方案:)

@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();
        }
    }
}


 类似资料:
  • 你好,我曾与JAX-WS合作开发基于SOAP的网络服务。现在我想使用REST,因为正如我从这里学习的那样,REST比SOAP有优势。 但从不同的文章中,我知道我们也可以从JAX-WS创建RESTful Web服务。但是大多数人说我们应该使用JAX-RS而不是JAX-WS。 我的问题是JAX-WS RESTful webservice和JAX-RS(泽西)之间有什么区别。JAX-RS相对于JAX-W

  • 这将给我一个错误,因为我不能使用@exceptionHandler(exception.class)两次。那么有什么解决办法呢?

  • 我需要构建一个前端web服务来接受和存储这些请求。 技术要求包括Spring boot和CXF。据我所知,CXF和Spring boot都能够接受http请求。 在spring boot中,您使用的控制器如下: 有人能帮助我理解处理http请求的这两种方法之间的根本区别吗?有没有一种方法可以在同一个应用程序中同时使用Spring Boot和CXF?

  • 我有一个可以激活和停用的资源()。 用于此目的的最适合REST的URLendpoint是什么? 现在我在考虑和,但我认为这不是很RESTful。 此外,我不确定使用什么HTTP方法。 你能提供一些指示吗? 谢谢!

  • 错误日志: 浏览器出错 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为java.lang.IllegalArgumentException:无效的处理程序方法返回值:false org.springframework.web.servlet.frameworkServlet.processRequest(framew

  • REST代表REpresentational State Transfer。REST是一种架构风格,而不是协议。 RESTful Web服务的优点 快速:RESTful Web服务很快,因为没有像SOAP这样严格的规范,它消耗更少的带宽和资源。 独立于语言和平台:RESTful Web服务可以用任何编程语言编写,并可在任何平台上执行。 可以使用SOAP:RESTful Web服务可以使用SOAP