【问题标题】:spring security with angularJS web app and android app带有 angularJS web 应用程序和 android 应用程序的 spring 安全性
【发布时间】:2018-02-11 14:08:30
【问题描述】:

我有一个 angualrJS 网络应用程序,它接收来自移动应用程序的警报并在谷歌地图上标记移动应用程序用户的位置。单击该标记后,信息窗口将打开用户名等。 BackEnd是java maven项目,使用spring boot。 我的问题是: 当我添加 Spring Boot 安全性时,为了验证页面,这会停止所有通信并且根本不会显示任何警报....

任何建议: 目前这是 application.js

var app = angular.module('sos',['ngRoute','mgcrea.ngStrap']);

app.config(['$httpProvider','$logProvider','$routeProvider',
function($httpProvider,$logProvider,$routeProvider) {
    $logProvider.debugEnabled(true);

    $routeProvider
    .when('/', { 
        controller: 'MainController',
        templateUrl: 'index.html',
        controllerAs: 'controller'
    })
    .when('/download',{
        templateUrl: 'download.html'
    })

当我启用 spring security 时,还有一个 websocket 连接也会停止并显示错误 500...

这是网络套接字配置:

@Configuration
 @EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer{

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(geoLocationHandler(), 
"/GeoLocationHandler").setAllowedOrigins("*");  
}       

@Bean
public GeoLocationHandler geoLocationHandler(){
    return new GeoLocationHandler();
}

有什么建议为什么 spring security 会停止 web 应用程序地图上显示的警报和标记?并且 spring security 会导致 websocket 失败并给出错误 500?

我尝试扩展 WebSecurityConfigAdapter 并将 mathcers 添加到主页等没有工作,我尝试了 cors 过滤器没有工作,我尝试了 csrf 过滤器也没有工作......任何建议将不胜感激......

【问题讨论】:

  • 您好,SO 和其他 SE 网站不是经典的论坛,它是问答形式的知识库,因为此类问题需要足够集中并提供问题中的所有元素。这意味着您应该在此处提供代码。
  • 您好,我在这里发布了代码:stackoverflow.com/questions/46091383/…

标签: android angularjs spring google-maps spring-security


【解决方案1】:

你可以试试这个 WebSecurityConfig.java 文件?

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().cors().disable().httpBasic().and().authorizeRequests()
    .antMatchers(PUBLIC_MATCHERS).permitAll().anyRequest().authenticated();

(我的 sn-p ,试试看,然后删除你不喜欢的) 我认为最终你不会让任何人获得。

这里我也给你我的全局方法,以防你需要它。

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

【讨论】:

  • 感谢 Tsakiroglou,这也将停止 GET,但我希望能够通过身份验证访问而不是收到错误,现在移动应用程序上的 webview 将无法打开。有没有办法我可以离开安全性,只要求用户名和密码来请求?
  • 你测试了吗?应该是.permitAll().anyRequest().authenticated();
  • 我能问你一件事吗?你是否保存会话以返回 xAuthentication???
  • 这就是我所拥有的:
  • @Override protected void configure(HttpSecurity http) throws Exception { http .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .csrf().disable().cors().disable ().httpBasic() .and().authorizeRequests() .antMatchers("/api/user/{userId}").permitAll().anyRequest().authenticated(); }
【解决方案2】:

好的,经过研究,我发现我需要包含 http 方法 get 才能在网络应用程序上接收警报,所以我的 websecurityConfiguration 是这样的:

    @Override
   protected void configure(HttpSecurity http) throws Exception {
    http
       .csrf().disable()                
       //.and()
       .httpBasic()
       .and()
       .authorizeRequests()                
       .antMatchers(HttpMethod.GET, "/*").permitAll()
       .antMatchers(HttpMethod.GET, "/api/geolocation/*").permitAll()
       .antMatchers(HttpMethod.GET, "/GeoLocationHandler").permitAll()
       .antMatchers(HttpMethod.GET, "/api/responser/*").permitAll()
       .antMatchers(HttpMethod.GET, "/api/user/").authenticated()
       .anyRequest().authenticated();                
}

否则 webapp 将不接受任何警报.... 希望这对需要它的人有所帮助....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 2011-07-04
    • 2011-07-29
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多