【发布时间】:2016-08-07 23:35:30
【问题描述】:
我正在通过分解this set of three interconnected apps at GitHub来研究Spring OAuth,同时也在仔细研究Spring OAuth 2 Developer Guide at this link。 开发者指南说/oauth/error 端点需要自定义,但是应该使用什么特定代码来成功覆盖/oauth/error?
第一次尝试:
我第一次尝试覆盖是抛出错误,您可以在the debug log, which I have uploaded to a file sharing site at this link 中看到。
我首先尝试让 Spring 提供的代码元素在示例应用程序中工作。
首先,我在上面的示例应用程序链接中为authserver 应用程序添加了一个新的控制器类,并添加了一些the sample code from Spring's WhiteLabelErrorEndpoint.java, which you can read at this link。我的尝试如下:
package demo;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.HtmlUtils;
@Controller
public class CustomViewsController {
private static final String ERROR = "<html><body><h1>OAuth Error</h1><p>${errorSummary}</p></body></html>";
@RequestMapping("/oauth/error")
public ModelAndView handleError(HttpServletRequest request) {
Map<String, Object> model = new HashMap<String, Object>();
Object error = request.getAttribute("error");
// The error summary may contain malicious user input,
// it needs to be escaped to prevent XSS
String errorSummary;
if (error instanceof OAuth2Exception) {
OAuth2Exception oauthError = (OAuth2Exception) error;
errorSummary = HtmlUtils.htmlEscape(oauthError.getSummary());
}
else {
errorSummary = "Unknown error";
}
model.put("errorSummary", errorSummary);
return new ModelAndView(new SpelView(ERROR), model);
}
}
我添加了以下SpelView.java上面的链接使用的字符串处理程序类by copying the code from this link:
package demo;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.security.oauth2.common.util.RandomValueStringGenerator;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
/**
* Simple String template renderer.
*
*/
class SpelView implements View {
private final String template;
private final String prefix;
private final SpelExpressionParser parser = new SpelExpressionParser();
private final StandardEvaluationContext context = new StandardEvaluationContext();
private PlaceholderResolver resolver;
public SpelView(String template) {
this.template = template;
this.prefix = new RandomValueStringGenerator().generate() + "{";
this.context.addPropertyAccessor(new MapAccessor());
this.resolver = new PlaceholderResolver() {
public String resolvePlaceholder(String name) {
Expression expression = parser.parseExpression(name);
Object value = expression.getValue(context);
return value == null ? null : value.toString();
}
};
}
public String getContentType() {
return "text/html";
}
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
Map<String, Object> map = new HashMap<String, Object>(model);
String path = ServletUriComponentsBuilder.fromContextPath(request).build()
.getPath();
map.put("path", (Object) path==null ? "" : path);
context.setRootObject(map);
String maskedTemplate = template.replace("${", prefix);
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(prefix, "}");
String result = helper.replacePlaceholders(maskedTemplate, resolver);
result = result.replace(prefix, "${");
response.setContentType(getContentType());
response.getWriter().append(result);
}
}
【问题讨论】:
-
为什么要使用 SpelView 进行自定义渲染?大多数人会使用诸如 Freemarker 或 Thymeleaf 或 Mustache 或 Groovy 之类的模板语言(Spring Boot 开箱即用地支持所有这些语言)。
-
@DaveSyer 我更喜欢所有视图都在 AngularJS 中。您的一些示例使用了 AngularJS,我想使用它。我的问题是在哪里可以找到一个很好的工作示例。我仔细阅读了 OP 中链接中的开发者指南,并仔细查看了示例应用程序中的代码。我做了谷歌搜索。但也许我的搜索关键词不正确,因为我找不到一个有效的例子。您愿意向我指出一个覆盖
/error端点的工作示例吗? -
@DaveSyer 我在 OP 中尝试了这种方法,因为它是谷歌搜索的结果。如果您愿意提供一个链接,我很乐意将它扔掉以支持可定制的工作示例。
-
您已经链接到的教程中有一个 /oauth/authorize 端点的 freemarker 实现。错误端点并没有什么不同。
-
@DaveSyer 您是指示例应用程序中的
login.ftl,还是我上面链接中开发人员指南的Customizing The UI部分?我在开发人员指南中的任何地方都没有发现freemarker这个词的任何使用,并且对eclipse 工作区的Ctrl-H全文搜索没有在任何应用程序的代码中显示对login.ftl一词的任何引用。澄清这是如何工作的将不胜感激。这是login.ftl的链接:github.com/spring-guides/tut-spring-security-and-angular-js/…
标签: spring spring-mvc spring-security spring-boot spring-oauth2