如何检查Spring Security进行用户身份验证和Flex的获得角色?(How can I c

2019-07-30 15:35发布

我使用的弹簧,弹簧安全,BlazeDS的,Flex和弹簧弯曲。

我知道我可以调用channelSet.login()channelSet.logout()挂接到Spring Security进行认证。 channelSet.authenticated显然只知道当前的Flex会议,因为它总是开始了为 ,直到你调用channelSet.login()

我想做的事:

  1. 从Flex的检查以了解如果用户已经在一个会话。
  2. 如果是的话,我想他们的用户名和角色。

UPDATE
我只是想我要补充,我从使用的解决方案的细节brd6644下面的答案,所以,这可能是为别人谁看起来这件事更容易。 我用这个 StackOverflow的答案,使SecurityContext注射。 我不会改写在这一个答案的代码,所以去看看它的SecurityContextFacade

securityServiceImpl.java

public class SecurityServiceImpl implements SecurityService {
    private SecurityContextFacade securityContextFacade;

    @Secured({"ROLE_PEON"})
    public Map<String, Object> getUserDetails() {
        Map<String,Object> userSessionDetails = new HashMap<String, Object>();

        SecurityContext context = securityContextFacade.getContext();
        Authentication auth = context.getAuthentication();
        UserDetails userDetails = (UserDetails) auth.getPrincipal();

        ArrayList roles = new ArrayList();
        GrantedAuthority[] grantedRoles = userDetails.getAuthorities();
        for (int i = 0; i < grantedRoles.length; i++) {
            roles.add(grantedRoles[i].getAuthority());
        }

        userSessionDetails.put("username", userDetails.getUsername());
        userSessionDetails.put("roles", roles);
        return userSessionDetails;
    }
}


securityContext.xml

<security:http auto-config="true">
    <!-- Don't authenticate Flex app -->
    <security:intercept-url pattern="/flexAppDir/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <!-- Don't authenticate remote calls -->
    <security:intercept-url pattern="/messagebroker/amfsecure" access="IS_AUTHENTICATED_ANONYMOUSLY" />
</security:http>

<security:global-method-security secured-annotations="enabled" />

<bean id="securityService" class="ext.domain.project.service.SecurityServiceImpl">
    <property name="securityContextFacade" ref="securityContextFacade" />
</bean>
<bean id="securityContextFacade" class="ext.domain.spring.security.SecurityContextHolderFacade" />


flexContext.xml

<flex:message-broker>
    <flex:secured />
</flex:message-broker>

<flex:remoting-destination ref="securityService" />
<security:http auto-config="true" session-fixation-protection="none"/>


FlexSecurityTest.mxml

<mx:Application ... creationComplete="init()">

    <mx:Script><![CDATA[
        [Bindable]
        private var userDetails:UserDetails; // custom VO to hold user details

        private function init():void {
            security.getUserDetails();
        }

        private function showFault(e:FaultEvent):void {
            if (e.fault.faultCode == "Client.Authorization") {
                Alert.show("You need to log in.");
                // show the login form
            } else {
                // submit a ticket
            }
        }
        private function showResult(e:ResultEvent):void {
            userDetails = new UserDetails();
            userDetails.username = e.result.username;
            userDetails.roles = e.result.roles;
            // show user the application
        }
    ]]></mx:Script>

    <mx:RemoteObject id="security" destination="securityService">
        <mx:method name="getUserDetails" fault="showFault(event)" result="showResult(event)" />
    </mx:RemoteObject>

    ...
</mx:Application>

Answer 1:

如果您使用的春天BlazeDS的整合 ,您可以使用org.springframework.flex.security.AuthenticationResultUtils实现getUserDetails方法。

public Map<String, Object> getUserDetails() {  
 return AuthenticationResultUtils.getAuthenticationResult();
}


Answer 2:

我会写一个返回当前用户的角色信息的固定弹簧的服务方法。 让Flex应用程序调用,当应用程序启动。 如果您收到一个FaultEvent由于安全错误,然后提示用户进行身份验证和使用ChannelSet.login()。



Answer 3:

看到这个博客,我跟着这个春天有柔性模块,很好地解决了这个问题之前。 希望它会为你提供了几个挑这可能会有帮助。

GridShore博客



文章来源: How can I check Spring Security for user authentication and get roles from Flex?