spring boot 应用程序在 vaadin 上有一个 api 部分和一个接口。我想为 api 和 formlogin 界面配置 http basic。在这个选项中,只有http基本有效,formlogin不接受登录名和密码
protected void configure(HttpSecurity http) throws Exception {
http.requestCache()
.requestCache(new CustomRequestCache())
.and().authorizeRequests()
.antMatchers("/v2/api-docs",
"/configuration/ui",
"/swagger-resources/**",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**",
"/configuration/**",
"/swagger*/**",
"/console/**").permitAll()
.requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()
.antMatchers(HttpMethod.GET).hasAnyRole(COMMITTEE_MEMBER,BUSINESS_ADMINISTRATOR)
.antMatchers(HttpMethod.DELETE).denyAll()
.antMatchers(HttpMethod.HEAD).denyAll()
.antMatchers(HttpMethod.OPTIONS).denyAll()
.antMatchers(HttpMethod.PATCH).denyAll()
.antMatchers(HttpMethod.POST).denyAll()
.antMatchers(HttpMethod.PUT).denyAll()
.antMatchers(HttpMethod.TRACE).denyAll()
.anyRequest().authenticated().and()
.httpBasic().and()
.csrf().disable()
.addFilter(new AuthorizationFilter(authenticationManager(), userRepository))
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().formLogin()
.loginPage(LOGIN_URL).permitAll()
.loginProcessingUrl(LOGIN_PROCESSING_URL)
.failureUrl(LOGIN_FAILURE_URL)
.and().logout().logoutSuccessUrl(LOGOUT_SUCCESS_URL);
}
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user")
.password(passwordEncoder().encode("******"))
.roles(ADMINISTRATOR);
}
public UserDetailsService userDetailsService() {
UserDetails user =
User.withUsername("admin")
.password("{noop}*******")
.roles(ADMINISTRATOR)
.build();
return new InMemoryUserDetailsManager(user);
}
如何设置它们以进行联合活动?
通过创建几个 bean(WebSecurityConfigurerAdapter 的继承者)来配置对具有不同安全设置的不同系统组件的访问。在下面的示例中,创建了 2 个配置:第一个 - 数据库中的用户使用 jwt 令牌访问系统,第二个 - 未经授权的公共访问。
配置1
配置2
为此,您需要了解:
http.antMatcher(...)和实现的http.requestMathcers(...)。PublicConfig是在之前执行的PrivateConfig