Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

42.7. {"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

Oauth @RestController 一切正常, @Controller 提示如下

		
{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}		
		
		

程序如下

		
package api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class IndexController {

	public IndexController() {
		// TODO Auto-generated constructor stub
	}

	@GetMapping("/")
	public String index() {
		return "Helloworld!!!";

	}

	@GetMapping("/about")
	public String test() {
		return "Helloworld!!!";

	}
}				
		
		

分析 @Controller 不允许直接返回字符串,必须使用 @ResponseBody 或者 ModelAndView,下改后问题解决。

		
package api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/web")
public class IndexController {

	public IndexController() {
		// TODO Auto-generated constructor stub
	}

	@GetMapping("/")
	@ResponseBody
	public String index() {
		return "Helloworld!!!";

	}

	@GetMapping("/about")
	@ResponseBody
	public String test() {
		return "Helloworld!!!";

	}
}

		
		

同时 @EnableWebSecurity 需要忽略 @Controller 的映射 URL

			
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

	@Override
	public void configure(WebSecurity web) throws Exception {
		web.ignoring().antMatchers("/**/json").antMatchers("/about", "/", "/css/**");
	}
}