Spring   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring启动:使用oauth2保护api端点,同时拥有mvc UI页面大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试使用标准登录spring-boot mvc应用程序,同时使用oAuth2安全性公开一些API端点.
基本上我的要求如下:

如果用户点击主页(“/”),请检查它是否经过身份验证.
如果没有显示登录表单,则显示主页.
但是,用户还应该能够请求oauth身份验证令牌,并使用该令牌access / api / assignment / {iD}.

我可以让标准登录工作,我可以让oauth2工作,但我不能让他们一起工作.

这是我目前的配置:

WebSecurityConfig

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private Datasource datasource;

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(httpSecurity http) throws Exception {
        super.configure(http);        
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {    
        auth.jdbcAuthentication().datasource(this.datasourcE).passwordEncoder(new BCryptpasswordEncoder());
    }
}

OAuth2Config

@Configuration
@EnableresourceServer
@EnableAuthorizationServer
public class OAuth2Config {

protected static final String resource_id = "oauthdemo";

@Configuration
@EnableresourceServer
protected static class resourceServer extends resourceServerConfigurerAdapter {

    @Override
    public void configure(httpSecurity http) throws Exception {
      http          
      .httpBasic().disable()
      .authorizerequests()
        .antMatchers("/js/**","/css/**","/images/**","/webjars/**","/login").permitAll()
          .anyrequest().authenticated()
          .and()
      .formLogin()
          .loginPage("/login")
          .permitAll()
          .and()
      .logout()
          .permitAll();

    }

    @Override
    public void configure(resourceServerSecurityConfigurer resources) throws Exception {
        resources.resourcEID(resource_id);
    }
}


@Configuration
@EnableAuthorizationServer
protected static class AuthServer extends AuthorizationServerConfigurerAdapter {

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.allowFormAuthenticationForClients();
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager);
    }

    @Override
    public void configure(ClientDetailsserviceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("client")
                .authorizedGrantTypes("password","refresh_token")
                .authorities("ROLE_USER")
                .scopes("read")
                .resourcEIDs(resource_id)
                .secret("secret").accessTokenValiditySeconds(3600);
    }

}

}

问题是我在尝试打开主页时遇到以下错误(“/”)

required to access this resource
<>

它不会重定向登录页面.
我不需要这个页面受oauth2保护,但即使我直接进入登录页面(“/ login”,我可以访问)并提供凭据,我仍然会得到“完全身份验证是必需的”错误.
即使我禁用了基本的http身份验证.

有谁知道如何将普通用户UI与需要受OAuth2保护的api端点分开?

最佳答案
你能试试吗?

http          
      .authorizerequests()
        .antMatchers("/js/**","/login").permitAll()
         .antMatchers("/login").permitAll()
         .antMatchers("/home").authenticated();

虑/ home是需要授权的页面.

大佬总结

以上是大佬教程为你收集整理的Spring启动:使用oauth2保护api端点,同时拥有mvc UI页面全部内容,希望文章能够帮你解决Spring启动:使用oauth2保护api端点,同时拥有mvc UI页面所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。