Spring security oauth2.0 restful和表单登录配置

Spring security oauth2.0 restful和表单登录配置,spring,oauth-2.0,spring-data-rest,spring-security-oauth2,spring-java-config,Spring,Oauth 2.0,Spring Data Rest,Spring Security Oauth2,Spring Java Config,我有一个应用程序,可以从oauth2 alos或通过oauth2 alos登录。 但是我遇到了一些麻烦 我所遇到的: 当我访问时,它会进入/登录页面 当我访问时,它也会显示/登录页面到底出了什么问题。我想要的是我可以使用oauth2访问/api/hellos 以下是我的安全配置: @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = tru

我有一个应用程序,可以从oauth2 alos或通过oauth2 alos登录。 但是我遇到了一些麻烦

我所遇到的:

  • 当我访问时,它会进入/登录页面
  • 当我访问时,它也会显示/登录页面到底出了什么问题。我想要的是我可以使用oauth2访问/api/hellos
以下是我的安全配置:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private SpringDataMyBatisUserDetailsService userDetailsService;

    @Override
    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
        .userDetailsService(this.userDetailsService)
        .passwordEncoder(Manager.PASSWORD_ENCODER);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilterBefore(new CorsFilter(), ChannelProcessingFilter.class);
    }

    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

        private final AuthenticationManager authenticationManager;
        @Autowired
        private TokenStore tokenStore;
        @Autowired
        private SpringDataMyBatisClientDetailsService clientDetailsService;

        @Autowired
        public AuthorizationServerConfig(AuthenticationManager authenticationManager) {
            this.authenticationManager = authenticationManager;
        }

        /**
         * Defines the security constraints on the token endpoints /oauth/token_key and /oauth/check_token
         * Client credentials are required to access the endpoints
         *
         * @param oauthServer
         * @throws Exception
         */
        @Override
        public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
            oauthServer
            .tokenKeyAccess("permitAll()")
            .checkTokenAccess("isAuthenticated()");
        }

        /**
         * Defines the authorization and token endpoints and the token services
         *
         * @param endpoints
         * @throws Exception
         */
        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints
            .authenticationManager(this.authenticationManager)
            .tokenEnhancer(tokenEnhancer())
            .tokenStore(tokenStore);
        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients
            .withClientDetails(clientDetailsService);
        }

        @Bean
        public TokenEnhancer tokenEnhancer() {
            return new CustomTokenEnhancer();
        }

    }

    @Order(1)
    @Configuration
    public class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/index.html", "/index.css", "/common.js", "/index.js", "/api/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .and()
            .logout()
            .logoutSuccessUrl("/")
            .and().exceptionHandling().accessDeniedPage("/error/403");
        }

    }

    @Configuration
    @EnableResourceServer
    @Order(2)
    public class AuthorizationResourceConfig extends ResourceServerConfigurerAdapter {

        @Autowired
        private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
        @Autowired
        private AuthenticationSuccessHandler successHandler;
        @Autowired
        private AuthenticationFailureHandler failureHandler;
        @Autowired
        private TokenStore tokenStore;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources
            .stateless(true)
            .tokenStore(tokenStore);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
            .authorizeRequests()
            .and()
            .anonymous().disable()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and().httpBasic()
            .and()
            .exceptionHandling()
            .accessDeniedHandler(new OAuth2AccessDeniedHandler())
            .authenticationEntryPoint(restAuthenticationEntryPoint)
            .and()
            .authorizeRequests()
            .antMatchers("/api/**").fullyAuthenticated();

        }
    }

}
我尝试过一些从谷歌搜索的方法,但没有一种能帮到我。 所以,我真的希望有人能帮助我,我会感谢你


此外,我搜索到的最有用的信息是。

你使用这个“.antMatchers(“/api/**”).fullyaauthenticated();”,在这种情况下,所有操作都需要“登录”/api/,,如果你不想“127.0.0.1/api/hellos”的“登录”表单,你必须只使用/api/而不使用星号

你使用这个.antMatchers(“/api/**”).fullyaauthenticated();”,。在这种情况下,所有操作/api/…,都需要“登录”,,如果你不想“127.0.0.1/api/hellos”的“登录”表单,你必须只使用/api/不带星号

我不使用资源路径匹配。我已经删除了它。你使用这个“.antMatchers”(/api/**”).fullyaauthenticated();”,在这种情况下,所有操作都需要“登录”/api/,,如果你不想使用“”的“登录”表单,你必须只使用/api/而不使用stars,你可能会误解。我的项目包含两个不同的部分,一个JSF管理面板和一个RESTfull服务。我正在尝试根据用户导航的URL设置spring security以使用不同的身份验证方法。我没有使用资源路径匹配。我已将其删除。您使用此“.antMatchers”(/api/**”).FullYaAuthenticated();”,在这种情况下,所有操作/api/…,都需要“登录”,,如果你不想使用“”的“登录”表单,你必须只使用/api/而不使用stars,你可能会误解。我的项目包含两个不同的部分,一个JSF管理面板和一个RESTfull服务。我正在尝试设置SpringSecurity,根据用户导航的URL使用不同的身份验证方法。