gpt4 book ai didi

java - 从登录 URL 重定向

转载 作者:行者123 更新时间:2023-11-30 04:15:23 25 4
gpt4 key购买 nike

当有 session 时,我试图从登录页面重定向。类里面有没有选项org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint下面是我的 spring xml。请提供您的意见以实现这一目标

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<beans:bean id="springSecurityFilterChain"
class="org.springframework.security.web.FilterChainProxy">
<filter-chain-map path-type="ant">
<filter-chain pattern="/resources/template/img/**"
filters="none" />
<filter-chain pattern="/resources/template/css/**"
filters="none" />
<filter-chain pattern="/resources/template/js/**"
filters="none" />
<filter-chain pattern="/resources/template/misc/**"
filters="none" />
<filter-chain pattern="/resources/js/**"
filters="none" />
<filter-chain pattern="/resources/tiles/**"
filters="none" />
<filter-chain pattern="/resources/img/**"
filters="none" />
<filter-chain pattern="/**"
filters="
securityContextPersistenceFilter,
logoutFilter,
authenticationProcessingFilter,
exceptionTranslationFilter,
filterSecurityInterceptor" />
</filter-chain-map>

</beans:bean>

<beans:bean id="securityContextPersistenceFilter"
class="org.springframework.security.web.context.SecurityContextPersistenceFilter">
</beans:bean>

<beans:bean id="exceptionTranslationFilter"
class="org.springframework.security.web.access.ExceptionTranslationFilter">
<beans:property name="authenticationEntryPoint" ref="authenticationEntryPoint" />
<beans:property name="accessDeniedHandler" ref="accessDeniedHandler" />
</beans:bean>

<beans:bean id="authenticationEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<!-- <property name="loginFormUrl" value="/login.jsp?error=entryPoint"
/> -->
<beans:property name="loginFormUrl" value="/login" />
</beans:bean>

<beans:bean id="accessDeniedHandler"
class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
<!-- <property name="errorPage" value="/login.jsp?error=access_denied"
/> -->
<beans:property name="errorPage" value= "/loginfail" />
</beans:bean>

<beans:bean id="authenticationProcessingFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="authenticationFailureHandler">
<beans:bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<beans:property name="defaultFailureUrl" value="/loginfail" />
</beans:bean>
</beans:property>
<beans:property name="authenticationSuccessHandler">
<beans:bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
<beans:property name="defaultTargetUrl" value="/frame_design" />
</beans:bean>
</beans:property>
</beans:bean>

<beans:bean id="filterSecurityInterceptor"
class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="accessDecisionManager" ref="accessDecisionManager" />
<beans:property name="securityMetadataSource">
<filter-security-metadata-source
path-type="ant" id="securityDefinitionSource">
<intercept-url pattern="/frame_design*"
access="ADMIN" />

</filter-security-metadata-source>
</beans:property>
</beans:bean>


<beans:bean id="logoutFilter"
class="org.springframework.security.web.authentication.logout.LogoutFilter">
<beans:constructor-arg value="/logout" />
<beans:constructor-arg ref="logoutHandler">
</beans:constructor-arg>
</beans:bean>

<beans:bean id="logoutHandler"
class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler">

</beans:bean>
<authentication-manager alias="authenticationManager">
<authentication-provider ref="authenticationProvider">

</authentication-provider>
</authentication-manager>

<beans:bean id="authenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="employAuthDetailsService" />
<beans:property name="passwordEncoder" ref="passwordEncoder"/>
<beans:property name="saltSource" ref="saltSource"/>


</beans:bean>

<beans:bean id ="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" >
<beans:constructor-arg value="512"/>
<beans:property name="iterations" value="1024"/>
</beans:bean>
<beans:bean id="saltSource"
class="org.springframework.security.authentication.dao.ReflectionSaltSource">
<beans:property name="userPropertyToUse" value="username"></beans:property>

</beans:bean>

<beans:bean id="employAuthDetailsService" class="com.app.myapp.security.UserDetailsServiceImp">
</beans:bean>


<beans:bean id="accessDecisionManager"
class="org.springframework.security.access.vote.AffirmativeBased">
<beans:property name="decisionVoters">
<beans:list>
<beans:ref bean="roleVoter" />
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="roleVoter"
class="org.springframework.security.access.vote.RoleHierarchyVoter">
<beans:property name="rolePrefix" value="" />
<beans:constructor-arg ref="roleHierarchy" />
</beans:bean>

<beans:bean id="roleHierarchy"
class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl">
<beans:property name="hierarchy">
<beans:value>
ADMIN
<!-- ADMIN > ROLE_OWNER -->
<!-- ROLE_OWNER > ROLE_DISTRIBUTOR -->
<!-- ROLE_DISTRIBUTOR > ROLE_RESELLER -->
<!-- ROLE_RESELLER > ROLE_USER -->

</beans:value>
</beans:property>
</beans:bean>

</beans:beans>

最佳答案

防止已登录的用户再次访问登录页面。

  • 最干净的方法:您可以实现一个过滤器( HandlerInterceptorHandlerInterceptorAdapter ),重定向已登录的用户

  • 更多技巧:使用 <sec:authenticate>在登录页面中触发一个 java 脚本,当用户已经登录时重定向用户。

我的LoginPageRedirectInterceptor:

public class LoginPageRedirectInterceptor extends HandlerInterceptorAdapter {

private String[] loginPagePrefixes = new String[] { "/login" };

private String redirectUrl = "/";

private UrlPathHelper urlPathHelper = new UrlPathHelper();

@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {

if (isInLoginPaths(this.urlPathHelper.getLookupPathForRequest(request))
&& isAuthenticated()) {
response.setContentType("text/plain");
sendRedirect(request, response);
return false;
} else {
return true;
}
}


private boolean isAuthenticated() {
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
if (authentication == null) {
return false;
}
if (authentication instanceof AnonymousAuthenticationToken) {
return false;
}
return authentication.isAuthenticated();
}

private void sendRedirect(HttpServletRequest request,
HttpServletResponse response) {

String encodedRedirectURL = response.encodeRedirectURL(
request.getContextPath() + this.redirectUrl);
response.setStatus(HttpStatus.SC_TEMPORARY_REDIRECT);
response.setHeader("Location", encodedRedirectURL);
}

private boolean isInLoginPaths(String requestUrl) {

for (String login : this.loginPagePrefixes) {
if (requestUrl.startsWith(login)) {
return true;
}
}
return false;
}
}

关于java - 从登录 URL 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18545839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com