spring security通过URL传递

时间:2015-01-03 23:58:23

标签: spring-mvc spring-security spring-boot

我有一个使用spring-security和spring-mvc的spring-boot 1.1.7应用程序。我有一个登录表单,大多数用户登录并访问该网站就好了。但是,我有一个新的要求,即他们想要提交网址请求并获取XML结果。我尝试添加一个允许绕过url的安全配置,但我的响应始终是我登录页面的HTML。

这是我的控制器:

@Controller(value = "/remote")
public class RemoteSearchController {

    @Autowired
    private SearchService searchService;

    @RequestMapping(value = "/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
     public List<Sdn> searchTreasury(@ModelAttribute SdnSearch sdnSearch, @PathVariable String username, @PathVariable String password, @PathVariable String sdnName, Authentication authentication) {

     // do some work and return a list of objects into XML
     ...
     }
  }

这是我的安全配置:

@Configuration
@EnableWebMvcSecurity
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

     http
         .authorizeRequests()
          .antMatchers( "/remote/search").permitAll().anyRequest();
    }

// adding this method results in a 404 - otherwise a login html is returned.
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers(
                        "/remote/search**",
                        "/remote/search/**" );

        return;
    }

然后使用REST客户端我提交:

http://localhost:9001/remote/search?username=user&password=password&sdnName=Victoria

我想也许会收到用户名和密码进行身份验证。但显然这种方法永远不会受到打击。并且,覆盖配置(WebSecurity Web)会产生404。

更新: Per Dave S,我将我的Controller改为Controller而不是bean:

@RequestMapping(value = "/remote/search{username}{password}", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
    public List<Sdn> search(@ModelAttribute SdnSearch sdnSearch, @PathVariable String username, @PathVariable String password,  Authentication authentication) {
 List<Sdn> foundSdns = sdnSearchService.find( sdnSearch );
        auditService.logSearchResults( authentication.getName() );
        return foundSdns;
}

与我的安全配置相结合,这允许未经身份验证的搜索,但用户名,密码和身份验证参数为空,因此我无法手动进行身份验证。

如果这将是真正的REST,我似乎需要提交两个请求,一个用于令牌,另一个用于在头中搜索该令牌。但我不确定如何以客户可以通过URL请求轻松完成此操作来设置它。

0 个答案:

没有答案