我对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;
}
}
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");
}
}
任何帮助将不胜感激。
我将registry.addViewController更改为以下内容。 registry.addViewController( “/”)setViewName( “静态/视图/ index.html中”); –
我还在项目中添加了项目结构。 –
太好了。更新我的回答 –