我正在研究this set of three interconnected apps at GitHub來研究Spring OAuth,同時也仔細研究了Spring OAuth 2 Developer Guide at this link。 開發者指南指出/oauth/error
端點需要自定義,但是應該使用什麼特定的代碼才能成功覆蓋/oauth/error
?如何覆蓋這個`/ 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);
}
}
並且我增加所使用的鏈路上述by copying the code from this link以下SpelView.java
字符串處理類:
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或Moustache或Groovy這樣的模板語言(所有這些語言都是Spring Boot的開箱即用的支持)。 –
@DaveSyer我更喜歡AngularJS中的所有視圖。你的一些例子使用了AngularJS,我想繼續這樣做。我的問題在於我能找到一個好的工作示例。我仔細閱讀了OP中鏈接中的開發者指南,並仔細閱讀了示例應用程序中的代碼。我做了谷歌搜索。但也許我的關鍵詞我的搜索是不正確的,因爲我找不到一個工作的例子。你是否願意指向我一個重寫'/ error'端點的工作示例。? – CodeMed
@DaveSyer我在OP中嘗試了這種方法,因爲它是谷歌搜索的結果。如果你願意給出一個可定製的工作示例,我會非常樂意拋棄它。 – CodeMed