[英]Spring 4 Security Tiles 3 Custom Success Handler
我花了點時間弄清楚如何重定向到tile配置中定義的頁面。
將Spring Security 4與注釋和Tiles一起使用。
下面的CustomSuccessHandler
可以工作,但不能將targetUrl
解析為tile配置中定義的頁面。
@Component
public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Override
protected void handle(HttpServletRequest request,
HttpServletResponse response, Authentication authentication) throws IOException {
String targetUrl = determineTargetUrl(authentication);
if (response.isCommitted()) {
System.out.println("Can't redirect");
return;
}
test();
redirectStrategy.sendRedirect(request, response, targetUrl);
}
static void test() {
}
protected String determineTargetUrl(Authentication authentication) {
String url="";
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
List<String> roles = new ArrayList<String>();
for (GrantedAuthority a : authorities) {
roles.add(a.getAuthority());
}
if (isAdmin(roles)) {
url = "/admin";
} else if (isUser(roles)) {
url = "/user";
} else {
url="accessDenied";
}
return url;
}
我發現自己的問題像往常一樣是自欺欺人的。 我忽略了在views.xml(文件配置)文件中定義“管理員”或“用戶”。 一旦我在views.xml中配置了頁面,它就會按預期開始工作。 謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.