私は春のセキュリティには新しく、4つの異なる役割を持つWebアプリケーションを構築しています。春は許可されたユーザーに異なる役割を使用しますが、 4種類のサインアップとサインイン後、ユーザーがadminとして入力した場合のように4つの異なるページにユーザーを送信する必要があります。顧客の場合は、顧客ページに送信したい場合は管理者ページに送信します。さまざまな種類のユーザーによるSpringセキュリティ認証
これまでのところ、ユーザー詳細サービスを使用して1人のユーザーを認証できました。
私は春のセキュリティには新しく、4つの異なる役割を持つWebアプリケーションを構築しています。春は許可されたユーザーに異なる役割を使用しますが、 4種類のサインアップとサインイン後、ユーザーがadminとして入力した場合のように4つの異なるページにユーザーを送信する必要があります。顧客の場合は、顧客ページに送信したい場合は管理者ページに送信します。さまざまな種類のユーザーによるSpringセキュリティ認証
これまでのところ、ユーザー詳細サービスを使用して1人のユーザーを認証できました。
設定では、4種類のロールを定義し、カスタムログイン成功ハンドラを追加します。以下の例を参照してください。
<http use-expressions="true">
<intercept-url pattern="/login*" access="permitAll"/>
<intercept-url pattern="/**" access="isAuthenticated()"/>
<form-login login-page='/login.html'
authentication-failure-url="/login.html?error=true"
authentication-success-handler-ref="myAuthenticationSuccessHandler"/>
<logout/>
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="user1" password="user1Pass" authorities="ROLE_USER"/>
<user name="admin1" password="admin1Pass" authorities="ROLE_ADMIN"/>
<user name="customer1" password="customer1Pass" authorities="ROLE_CUSTOMER"/>
<user name="other1" password="other1Pass" authorities="ROLE_OTHER"/>
</user-service>
</authentication-provider>
</authentication-manager>
<beans:bean id="myAuthenticationSuccessHandler"
class="org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler"/>
そしてAuthenticationSuccessHandlerはこのように書き:
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
protected Log logger = LogFactory.getLog(this.getClass());
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException {
handle(request, response, authentication);
clearAuthenticationAttributes(request);
}
protected void handle(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException {
String targetUrl = determineTargetUrl(authentication);
if (response.isCommitted()) {
logger.debug(
"Response has already been committed. Unable to redirect to "
+ targetUrl);
return;
}
redirectStrategy.sendRedirect(request, response, targetUrl);
}
protected String determineTargetUrl(Authentication authentication) {
boolean isUser = false;
boolean isAdmin = false;
boolean isCustomer = false;
boolean isOther = false;
Collection<? extends GrantedAuthority> authorities
= authentication.getAuthorities();
for (GrantedAuthority grantedAuthority : authorities) {
if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
isUser = true;
break;
} else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
isAdmin = true;
break;
} else if (grantedAuthority.getAuthority().equals("ROLE_CUSTOMER")) {
isCustomer = true;
break;
} else if (grantedAuthority.getAuthority().equals("ROLE_OTHER")) {
isOther = true;
break;
}
}
if (isUser) {
return "/user.html";
} else if (isAdmin) {
return "/admin.html";
} else if (isCustomer) {
return "/customer.html";
} else if (isOther) {
return "/other.html";
} else {
throw new IllegalStateException();
}
}
protected void clearAuthenticationAttributes(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if (session == null) {
return;
}
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
}
public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
this.redirectStrategy = redirectStrategy;
}
protected RedirectStrategy getRedirectStrategy() {
return redirectStrategy;
}
}
別の方法としては、専用コントローラのマッピングにあなたの成功したログインページauthentication-success-handler-ref
をリダイレクトしています。
@RequestMapping("/login/process")
public String processLogin() {
Collection<GrantedAuthority> authorities = (Collection<GrantedAuthority>)
SecurityContextHolder.getContext().getAuthentication().getAuthorities();
if (authorities.contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) {
return "redirect:/admin-page";
} else if (authorities.contains(new SimpleGrantedAuthority("ROLE_USER_1"))) {
return "redirect:/user_1";
} else if (authorities.contains(new SimpleGrantedAuthority("ROLE_USER_2"))) {
return "redirect:/user_2";
} else if (authorities.contains(new SimpleGrantedAuthority("ROLE_USER_3"))) {
return "redirect:/user_3";
}
// catch else
return "redirect:/";
}
shiの応答に加えて、私はHandlerをいくつか変更します。ハンドラのクラスフィールドとしてMap<String,String>
を使用して、ロールリダイレクトのマッチングを管理できます。
そして、あなたも、それはすでに実装のメソッドを利用するには、このようorg.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandlerを拡張することができます:
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
public class MySimpleUrlAuthenticationSuccessHandler
extends SimpleUrlAuthenticationSuccessHandler
implements AuthenticationSuccessHandler {
protected Logger logger = Logger.getLogger(this.getClass());
private Map<String, String> authorityRedirectionMap;
public MySimpleUrlAuthenticationSuccessHandler(Map<String, String> authorityRedirectionMap) {
super();
this.authorityRedirectionMap = authorityRedirectionMap;
}
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
String targetUrl = determineTargetUrl(request, response, authentication);
if (response.isCommitted()) {
logger.debug("Response has already been committed. Unable to redirect to "
+ targetUrl);
return;
}
this.getRedirectStrategy().sendRedirect(request, response, targetUrl);
}
/**
*
* @param request
* @param response
* @param authentication
* @return
*/
protected String determineTargetUrl(HttpServletRequest request,
HttpServletResponse response, Authentication authentication) {
for (GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
if(this.authorityRedirectionMap.containsKey(grantedAuthority.getAuthority())){
return this.authorityRedirectionMap.get(grantedAuthority.getAuthority());
}
}
return super.determineTargetUrl(request, response);
}
}
あなたの構成XMLの成功ハンドラのセクションでは、このようにする必要があります:
<beans:bean id="myAuthenticationSuccessHandler"
class="org.baeldung.security.MySimpleUrlAuthenticationSuccessHandler">
<beans:constructor-arg>
<beans:map>
<beans:entry key="ROLE_USER" value="/user.html" />
<beans:entry key="ROLE_ADMIN" value="/admin.html" />
<beans:entry key="ROLE_CUSTOMER" value="/customer.html" />
<beans:entry key="ROLE_OTHER" value="/other.html" />
</beans:map>
</beans:constructor-arg>
<beans:property name="defaultTargetUrl" value="/default.html" />
</beans:bean>
を