Spring框架和AngularJs的圆形视图路径异常 [英] Circular view path exception with Spring framework And AngularJs
问题描述
我对 Spring 引导框架工作比较陌生.我有一个在 Angular 中构建的基本 Web 应用程序,它连接了 Spring Boot 并连接到了 Mongodb.该应用程序允许用户添加待办事项列表并注册网站.当应用程序启动时,它将存储在 mongodb 中的 todolist 返回到视图中.用户可以注册,并将详细信息存储在 Mongo 存储库中.
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.
当我添加并实现 spring security 时,我收到了错误消息
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.)
我想要发生的是,当 webapp 加载时,我希望 index.html 被注入 todo.html.然后,如果用户登录,他们将被定向到另一个页面或某些 Ui 功能可用.目前我被困在这个圆形视图路径循环中.
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.
我已经查看了不同的答案,但我仍然对导致问题的确切原因感到困惑.我相信它在 WebSecurityConfig 类
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);
}
}
资源控制器
@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");
}
}
任何帮助将不胜感激.
这是项目布局.
推荐答案
您忘记将 .html
添加到您的视图名称中:
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 注册了一个 ResourceHttpRequestHandler
,它能够解析 static
文件夹下的静态资源.因为您将 login
设置为视图名称 ResourceHttpRequestHandler
尝试加载显然不存在的 static/login
.将其更改为 app/views/login.html
,以便 static/login
变为 static/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
.
这篇关于Spring框架和AngularJs的圆形视图路径异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!