【问题标题】:Authentication spring rest service in java [server side]java中的身份验证spring rest服务[服务器端]
【发布时间】:2014-07-10 02:35:12
【问题描述】:

我有一个 spring 项目,它向其他人公开了一些 api,但他们需要进行身份验证才能使用该服务。

@Configuration
@EnableWebSecurity
public class SecConfig extends WebSecurityConfigurerAdapter{
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("admin")
                .roles("USER");
    }
}

当我使用这个 spring 配置时,我能够使用下面的客户端项目来验证和使用服务

public void getRestValue() {
        final String url = "http://localhost:8080/template/getData";
        final String username = "admin@admin.com";
        final String password = "admin";
        // Populate the HTTP Basic Authentitcation header with the username and
        // password
        RestTemplate restTemplate = new RestTemplate();
        String plainCreds = username + ":" + password;
        byte[] plainCredsBytes = plainCreds.getBytes();
        byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
        String base64Creds = new String(base64CredsBytes);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + base64Creds);
        HttpEntity<String> request = new HttpEntity<String>(headers);
        ResponseEntity<String> response = restTemplate.exchange(url,
                HttpMethod.GET, request, String.class);
        System.out.println(response);
        String account = response.getBody();
        System.out.println(account);
    }

但是当我使用以下配置使用我的用户服务从数据库获取值时,我将登录页面作为响应

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    AppUserDetailsService appUserDetailsService;

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

    @Override
    public void configure(WebSecurity webSecurity) throws Exception {
        webSecurity.ignoring().antMatchers("/resources/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().antMatchers("/setup/*")
                .permitAll().antMatchers("/login").permitAll()
                .antMatchers("/logout").permitAll().anyRequest()
                .authenticated().and().formLogin().loginPage("/login")
                .loginProcessingUrl("/j_spring_security_check")
                .usernameParameter("j_username")
                .passwordParameter("j_password").failureUrl("/login")
                .defaultSuccessUrl("/").permitAll().and().logout()
                .logoutUrl("/j_spring_security_logout")
                .logoutSuccessUrl("/login").deleteCookies("JSESSIONID")
                .invalidateHttpSession(true);
    }
}

请告诉我哪里出错了

【问题讨论】:

  • 调试日志说什么?
  • 当我调用其余服务时,我会在响应正文中获得登录页面 html。

标签: java spring rest spring-mvc spring-security


【解决方案1】:

您的客户端使用 HTTP Basic 身份验证,但您的服务器仅配置为表单身份验证。尝试在您的 configure(HttpSecurity http) 方法中添加 .httpBasic()

【讨论】:

  • 我也尝试添加它。在这种情况下,当我在浏览器中尝试时,即使可用的身份验证也被禁用。
猜你喜欢
  • 2014-12-25
  • 2013-06-22
  • 1970-01-01
  • 2017-08-15
  • 2020-06-24
  • 1970-01-01
  • 2019-07-30
  • 2018-02-26
  • 2013-11-12
相关资源
最近更新 更多