2017-02-14 40 views
0

我對Spring引導框架工作比較陌生。我有一個基於Angular構建的基本Web應用程序,連接了Spring引導程序和一個Mongodb。該應用程序允許用戶添加待辦事項列表並註冊該網站。當應用程序啓動時,它將存儲在mongodb中的todolists返回到視圖。用戶可以註冊,並有詳細信息存儲在一個Mongo倉庫。Spring框架與AngularJs的圓形視圖路徑異常

當我加入並實施春季安全,我得到了錯誤信息

Circular view path [login]: would dispatch back to the current handler URL [/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) 

我希望發生的是,當Web應用程序的負載,我想中的index.html與todo.html注入。然後,如果用戶登錄,他們將被引導到另一個頁面或某個UI功能可用。此刻我卡在這圓形視圖路徑循環。

我已經查看了不同的答案,但我仍然對究竟是什麼導致問題感到困惑。我相信這是在WebSecurityConfig

@Configuration 
@EnableWebMvcSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ 

    @Autowired 
    UserDetailsService userDS; 

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

    http 
     .authorizeRequests() 
     .antMatchers("/api/todos/*").permitAll() 
     .anyRequest().authenticated() 
     .and() 
     .formLogin() 
     .loginPage("/login") 
     .permitAll() 
     .and() 
     .logout() 
     .permitAll(); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 

    auth 
     .userDetailsService(userDS); 

    } 

    @Override 
    protected UserDetailsService userDetailsService() { 
     return userDS; 
    } 
} 

AuthUserDetailsS​​ervice

@Repository 
public class AuthUserDetailsService implements UserDetailsService { 
    @Autowired 
    private UserRepository users; 
    private org.springframework.security.core.userdetails.User userdetails; 

    @Override 
    public UserDetails loadUserByUsername(String username) 
     throws UsernameNotFoundException { 
    // TODO Auto-generated method stub 

    boolean enabled = true; 
    boolean accountNonExpired = true; 
    boolean credentialsNonExpired = true; 
    boolean accountNonLocked = true; 

    todoapp.models.User user = getUserDetail(username); 

    userdetails = new User (user.getUsername(), 
       user.getPassword(), 
       enabled, 
       accountNonExpired, 
       credentialsNonExpired, 
       accountNonLocked, 
       getAuthorities(user.getRole()) 
       ); 

    return userdetails; 
    } 
    public List<GrantedAuthority> getAuthorities(Integer role) { 

    List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(); 
    if (role.intValue() == 1) { 
     authList.add(new SimpleGrantedAuthority("ROLE_ADMIN")); 
    } else if (role.intValue() == 2) { 
     authList.add(new SimpleGrantedAuthority("ROLE_USER")); 
    } 

    return authList; 
    } 

    private todoapp.models.User getUserDetail(String username){ 

     todoapp.models.User user = users.findByUsername(username); 


    return user; 
    } 
} 

TodoController

@RestController 
@RequestMapping("/api/todos") 
public class TodoController { 

    @Autowired 
    TodoRepository todoRepository; 

    @RequestMapping(method=RequestMethod.GET) 
    public List<Todo> getAllTodos() { 
     return todoRepository.findAll(); 
    } 

    @RequestMapping(method=RequestMethod.POST) 
    public Todo createTodo(@Valid @RequestBody Todo todo) { 
     return todoRepository.save(todo); 
    } 

    @RequestMapping(value="{id}", method=RequestMethod.GET) 
    public ResponseEntity<Todo> getTodoById(@PathVariable("id") String id) { 
     Todo todo = todoRepository.findOne(id); 
     if(todo == null) { 
      return new ResponseEntity<Todo>(HttpStatus.NOT_FOUND); 
     } else { 
      return new ResponseEntity<Todo>(todo, HttpStatus.OK); 
     } 
    } 

    @RequestMapping(value="{id}", method=RequestMethod.PUT) 
    public ResponseEntity<Todo> updateTodo(@Valid @RequestBody Todo todo, @PathVariable("id") String id) { 
     Todo todoData = todoRepository.findOne(id); 
     if(todoData == null) { 
      return new ResponseEntity<Todo>(HttpStatus.NOT_FOUND); 
     } 
     todoData.setTitle(todo.getTitle()); 
     todoData.setCompleted(todo.getCompleted()); 
     Todo updatedTodo = todoRepository.save(todoData); 
     return new ResponseEntity<Todo>(updatedTodo, HttpStatus.OK); 
    } 

    @RequestMapping(value="{id}", method=RequestMethod.DELETE) 
    public void deleteTodo(@PathVariable("id") String id) { 
     todoRepository.delete(id); 
    } 

} 

RecourceController

@Configuration 
public class ResourceController extends WebMvcConfigurerAdapter{ 
    @Override 
     public void addViewControllers(ViewControllerRegistry registry) { 
      registry.addViewController("/").setViewName("index"); 
      registry.addViewController("/api/todos").setViewName("home"); 
      registry.addViewController("/register").setViewName("register"); 
      registry.addViewController("/login").setViewName("login"); 
     } 
} 

任何幫助將不勝感激。

這是項目佈局。 enter image description here

回答

1

你忘了添加.html到您的視圖名稱:

registry.addViewController("/").setViewName("app/views/index.html"); 
registry.addViewController("/api/todos").setViewName("app/views/home.html"); 
registry.addViewController("/register").setViewName("app/views/register.html"); 
registry.addViewController("/login").setViewName("app/views/login.html"); 

春季啓動註冊表ResourceHttpRequestHandler它能夠static文件夾下解決靜態資源。 因爲您將login設置爲查看名稱ResourceHttpRequestHandler嘗試加載static/login,這顯然不存在。 將其更改爲app/views/login.html,以使static/login變爲static/app/views/login.html

+0

我將registry.addViewController更改爲以下內容。 registry.addViewController( 「/」)setViewName( 「靜態/視圖/ index.html中」); –

+0

我還在項目中添加了項目結構。 –

+0

太好了。更新我的回答 –