Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

mongodb - Circular view path exception with Spring framework And AngularJs

I am relatively new to the Spring boot frame work. I had a basic web application built in Angular with Spring boot connected and to a Mongodb. The application allowed users to add todo lists and register for the the website. When the application started it returned the todolists stored in mongodb to the view. The user could register, and there details were stored in a Mongo repository.

When I added and implemented spring security I got the error message

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

What I want to happen is, when the webapp loads, I want the index.html to be injected with todo.html. Then if a user logs in they will be directed to another page or some Ui feature to become available. At the moment I am stuck in this Circular view pathloop.

I have looked through the different answers but I still am confused as to what exactly is causing the issue. I believe it is in the WebSecurityConfig class

@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;
  }  
}

AuthUserDetailsService

@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");
      }
}

Any help will be greatly appreciated.

This is the Project Layout. enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You forgot to add .html to your view names:

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 registers a ResourceHttpRequestHandler which is capable of resolving static resources under static folder. Because you set login as view name ResourceHttpRequestHandler tries to load static/login which apparently does not exist. Change it to app/views/login.html so that static/login becomes static/app/views/login.html.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...