繁体   English   中英

带有HTML页面和Rest Web服务身份验证的Spring Security

[英]Spring security with Html pages and Rest web service authentication

我在应用程序中使用Spring安全性和数据库登录(将来我将必须实现LDAP身份验证)。 通过网络,一切正常,但是现在当我从外部调用网络服务时(我有一些用于内部javascript的网络服务,还有一些用于外部调用的网络服务),我收到登录页面的HTML代码。 没错,但是现在如何进行REST调用? 我必须保护它们,我想为每个Web服务调用使用令牌或用户名和密码,但是如何在REST调用中设置用户名和密码? 例如邮递员。 然后,我还将在

restTemplate.setRequestFactory(requestFactory);
responseEntity  = restTemplate.getForEntity(serverIp + "ATS/client/file/?filePath={filePath}", byte[].class, filePath); 

和在

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
ContentBody cbFile = new FileBody(file);
ContentBody cbPath= new StringBody(toStorePath,ContentType.TEXT_PLAIN);
builder.addPart("file", cbFile);
builder.addPart("toStorePath",cbPath);
httppost.setEntity(builder.build());
CloseableHttpResponse httpResponse = httpClient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();

在Web上,我什至拥有用户的角色,也许我还必须将其用于Web服务。 感谢您的建议。 关于更新:正如@Gergely Bacso所建议的那样,我已经更新了代码,但是现在我遇到了相反的问题:当我调用Web服务时,它们返回的所有信息都没有用户名和密码。 这是安全配置:

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

    @Autowired
    @Qualifier("userDetailsService")
    UserDetailsService userDetailsService;

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

    @Bean
    public PasswordEncoder passwordEncoder(){
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder;
    }

    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                    .antMatcher("/client/**")
                    .authorizeRequests()
                        .anyRequest().authenticated()
                        .and()
                    .httpBasic();
        }
    }

    @Configuration
    @Order(2)
    public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        public void configure(WebSecurity web) throws Exception {
            web
                    //Spring Security ignores request to static resources such as CSS or JS files.
                    .ignoring()
                        .antMatchers("/static/**");
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                    .authorizeRequests() //Authorize Request Configuration
                        //.antMatchers("/", "/register").permitAll()
                       // .antMatchers("/admin/**").hasRole("ADMIN")
                        .anyRequest().authenticated()
                        .and() //Login Form configuration for all others
                    .formLogin()
                        .loginPage("/login").permitAll();
        }
    }

}

几天前才问过一个类似的问题:

使用Spring Security保护REST服务

重要的部分是:

如果您想保护以编程方式访问的内容(例如,另一个程序正在调用REST服务),则不应使用基于表单的身份验证。

您需要的是更适合这份工作的东西。 像HTTP基本认证一样。 基于表单的登录方法更适合用户可以输入用户名/密码的用例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM