当前位置: 首页 > 知识库问答 >
问题:

Spring security如何在用户角色成功登录后重定向页面?

司空学智
2023-03-14

嗨,我需要知道如何通过验证用户角色来重定向应用程序,就像:如果ROLE_USER重定向到accountuser.xhtml或者如果ROLE_ADMIN重定向到accountadmin.xtml那样

使用spring security

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <display-name>iTubibe</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>

    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>1</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>Resources Servlet</servlet-name>
        <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Resources Servlet</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>charEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>charEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>excite-bike</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.FONT_AWESOME</param-name>
        <param-value>true</param-value>
    </context-param>
    <!-- Spring security filters -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/webflow
        http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

    <var name="user" class="com.ismart.itubibe.entities.UserEntity"/>

    <view-state id="welcome" view="welcome.xhtml" model="user">
        <transition on="newUser" to="signUp"/>
        <transition on="signIn" to="finish">
            <evaluate expression="userAuthenticationProviderService.processUserAuthentication(user)"/>
        </transition>
    </view-state>

    <view-state id="signUp" view="signUp.xhtml" model="user">
        <transition on="backToSignIn" to="welcome"/>
        <transition on="signUp" to="authentication">
            <evaluate expression="userServices.createUser(user)"/>
        </transition>
    </view-state>

    <action-state id="authentication">
        <evaluate expression="userAuthenticationProviderService.processUserAuthentication(user)"/>
        <transition on="yes" to="finish" />
        <transition on="no" to="welcome" />
    </action-state>

    <end-state id="finish" view="externalRedirect:account" />

</flow>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/security
            http://www.springframework.org/schema/security/spring-security.xsd">

    <security:http auto-config="true">
        <security:form-login login-page="/app/main" default-target-url="/app/account" />
        <security:logout logout-url="/app/logout" logout-success-url="/app/main" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider user-service-ref="userServices">
            <security:password-encoder hash="md5" />
        </security:authentication-provider>
    </security:authentication-manager>

    <bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <property name="userDetailsService" ref="userServices" />
        <property name="hideUserNotFoundExceptions" value="false" />
    </bean>

    <bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
        <constructor-arg>
            <ref bean="daoAuthenticationProvider" />
        </constructor-arg>
    </bean>

</beans>
 public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

                UserEntity user = userDao.loadUserByUserName(userName);

                if(user == null){
                    throw new UsernameNotFoundException(String.format("Etulisateur introuvable '%s'", userName)); 
                }

                Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

                authorities.add(new SimpleGrantedAuthority(user.getUserType()));

                User userDetails = new User(user.getUserName(), user.getPassWord(), authorities);

                return userDetails;
            }

和身份验证方法

public boolean processUserAuthentication(UserEntity user) {
        try {
            Authentication request = new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPassWord());
            Authentication result = authenticationManager.authenticate(request);
            SecurityContextHolder.getContext().setAuthentication(result);
            return true;
        } catch (AuthenticationException e) {

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "Sorry!!"));
            return false;
        }



    }

这是我的账户流水:

<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/webflow
        http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

    <secured attributes="ROLE_USER, ROLE_ADMIN" match="any"/>

    <view-state id="admin" view="accountAdmin.xhtml">
    </view-state>

    <view-state id="user" view="accountUser.xhtml">
    </view-state>




</flow>

共有1个答案

权承
2023-03-14

使用具有权限的分层角色肯定是一条可以走的路。这里有一个例子。

 类似资料:
  • 我正在我是身份提供者的地方实施SSO,现在我能够成功登录到服务提供者。但它把我带到了主页。我想在发布响应时指定着陆页URL。搜索了很多,但找不到任何令人信服的东西。不太知道SAML响应的哪个元素携带着陆页URL或采用我必须指定的形式。使用java和opensaml库生成响应。

  • 根据OneLogin留档,通过API调用登录用户的最后步骤是: 生成session_token并通过表单POST将其提交到OneLogin url 然后OneLogin服务器将为您启动会话,并向您的浏览器返回一个仅适用于httpOnly的域特定cookie 识别表单POST返回了302重定向状态,并将用户发送到另一个页面 我的问题是你应该如何处理第三个问题?尤其是对于返回的cookie。 文档指出

  • MvcConfig方法如下所示:

  • 下面是我的app.js代码,Login.js登录页面位于http://localhost:3000/Login,所以如果用户成功登录,我如何将用户重定向到http://localhost:3000/home page,它加载了“homeComponent.jsx”。为了简洁起见,我省略了“homeComponent.jsx”的代码

  • 我正在尝试使用MVC客户端设置IdentityServer4。 一切正常,直到我想添加ASP身份。当我添加代码以使用SQL server和Identity时,成功登录后,Identity server不会将我重定向回客户端,但它只是“刷新”页面。 IdentityServer应用程序启动: 在IdentityServer中配置 在MVC客户端中启动: 来自IdentityServer的日志: 我只

  • 我想在成功登录后为特定页面重定向用户。 我不希望用户在登录后导航到上次查看的页面。 我试过以下网址,但它显示我的错误。 错误: