Spring框架和AngularJs的圆形视图路径异常

时间:2017-02-14 13:03:58

标签: mongodb spring-boot spring-security spring-data

我对Spring引导框架工作相对较新。我有一个基本的Web应用程序,内置在Angular中,Spring引导连接到Mongodb。该应用程序允许用户添加待办事项列表并注册该网站。当应用程序启动时,它将存储在mongodb中的todolists返回到视图。用户可以注册,并且详细信息存储在Mongo存储库中。

当我添加并实现spring security时,我收到了错误消息

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.)

我想要发生的是,当webapp加载时,我希望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 个答案:

答案 0 :(得分: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");

Spring Boot注册ResourceHttpRequestHandler,它能够解析static文件夹下的静态资源。 因为您将login设置为视图名称ResourceHttpRequestHandler尝试加载显然不存在的static/login。 将其更改为app/views/login.html,以便static/login变为static/app/views/login.html