Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序上的Spring Max会话,而不是应用程序上用户的Max会话_Java_Spring_Session_Spring Security - Fatal编程技术网

Java 应用程序上的Spring Max会话,而不是应用程序上用户的Max会话

Java 应用程序上的Spring Max会话,而不是应用程序上用户的Max会话,java,spring,session,spring-security,Java,Spring,Session,Spring Security,我正在使用jhipster编写一个web应用程序。它使用的是弹簧。我试图限制同一用户登录到我的应用程序的次数,并使用以下命令在名为ServerConfiguration.java的文件上运行该应用程序: @Override protected void configure(HttpSecurity http) throws Exception { http .and() .formLogin() .loginProcessingUrl("/api

我正在使用jhipster编写一个web应用程序。它使用的是弹簧。我试图限制同一用户登录到我的应用程序的次数,并使用以下命令在名为
ServerConfiguration.java的文件上运行该应用程序:

 @Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .and()
        .formLogin()
        .loginProcessingUrl("/api/authentication")
        .successHandler(ajaxAuthenticationSuccessHandler)
        .failureHandler(ajaxAuthenticationFailureHandler)
        .usernameParameter("j_username")
        .passwordParameter("j_password")
        .permitAll()
    .
    .
    .
    .
    .and()            
        .sessionManagement()
        .maximumSessions(Integer.parseInt(env.getProperty("spring.maxuser.sessions")))
                 .maxSessionsPreventsLogin(true);
    }


@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
    return new HttpSessionEventPublisher();
}
这使得某个特定用户只需登录我的应用程序这么多次

现在,我的问题是如何使我的应用程序只对
x
不同的
用户开放/访问。例如,我希望我的应用程序只能被200个用户访问。当用户201出现并想要登录时,它就不能了

我在另一篇帖子上看到了答案,但我不知道该把代码放在哪里

public class MySessionAuthenticationStrategy extends ConcurrentSessionControlStrategy {
int MAX_USERS = 1000; // Whatever
SessionRegistry sr;

public MySessionAuthenticationStrategy(SessionRegistry sr) {
    super(sr);
    this.sr = sr;
}

@Override
public void onAuthentication(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
    if (sr.getAllPrincipals().size() > MAX_USERS) {
        throw new SessionAuthenticationException("Maximum number of users exceeded");
    }
    super.onAuthentication(authentication, request, response);
}
}

我应该创建这个新类
MySessionAuthenticationStrategy
,如何从我的httpConfigure类转到这个新类
MySessionAuthenticationStrategy

非常感谢。

试试这个。 创建一个类以扩展默认会话注册表:

@Component
public class MySessionRegistry extends org.springframework.security.core.session.SessionRegistryImpl {    
}
更新您的configure方法,使其如下所示

    @Autowired
    MySessionRegistry sessionRegistry; 
    void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginProcessingUrl("/api/authentication")
                .successHandler(ajaxAuthenticationSuccessHandler)
                .failureHandler(ajaxAuthenticationFailureHandler)
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                .permitAll().and()
                .sessionManagement()
                .maximumSessions(Integer.parseInt(env.getProperty("spring.maxuser.sessions")))
                .sessionRegistry(sessionRegistry)
                .maxSessionsPreventsLogin(true);
    }
然后在登录/身份验证期间,尝试以下操作:

    @Autowired
    MySessionRegistry sessionRegistry; 

    public void onAuthentication(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
        if (calculateMaxSessions(sessionRegistry) > MAX_USERS) {
            throw new SessionAuthenticationException("Maximum number of users exceeded");
        } else {
            //Authenticate
        }
    }

    public int calculateMaxSessions(SessionRegistry sessionRegistry){
        final List<Object> principals = sessionRegistry.getAllPrincipals();
        if (principals != null) {
            List<SessionInformation> sessions = new ArrayList<>();
            for (Object principal : principals) {
                sessions.addAll(sessionRegistry.getAllSessions(principal, false));
            }
            return sessions.size();
        }
        return 0;
    }
@Autowired
MySessionRegistry sessionRegistry;
身份验证(身份验证、HttpServletRequest请求、HttpServletResponse响应)上的公共无效{
if(calculateMaxSessions(会话注册表)>最大用户数){
抛出新的SessionAuthenticationException(“超出最大用户数”);
}否则{
//鉴定
}
}
public int calculateMaxSessions(会话注册表会话注册表){
最终列表主体=sessionRegistry.getAllPrinciples();
if(主体!=null){
列表会话=新建ArrayList();
for(对象主体:主体){
sessions.addAll(sessionRegistry.getAllSessions(主体,false));
}
return sessions.size();
}
返回0;
}
我希望这有帮助。干杯

试试这个。 创建一个类以扩展默认会话注册表:

@Component
public class MySessionRegistry extends org.springframework.security.core.session.SessionRegistryImpl {    
}
更新您的configure方法,使其如下所示

    @Autowired
    MySessionRegistry sessionRegistry; 
    void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginProcessingUrl("/api/authentication")
                .successHandler(ajaxAuthenticationSuccessHandler)
                .failureHandler(ajaxAuthenticationFailureHandler)
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                .permitAll().and()
                .sessionManagement()
                .maximumSessions(Integer.parseInt(env.getProperty("spring.maxuser.sessions")))
                .sessionRegistry(sessionRegistry)
                .maxSessionsPreventsLogin(true);
    }
然后在登录/身份验证期间,尝试以下操作:

    @Autowired
    MySessionRegistry sessionRegistry; 

    public void onAuthentication(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
        if (calculateMaxSessions(sessionRegistry) > MAX_USERS) {
            throw new SessionAuthenticationException("Maximum number of users exceeded");
        } else {
            //Authenticate
        }
    }

    public int calculateMaxSessions(SessionRegistry sessionRegistry){
        final List<Object> principals = sessionRegistry.getAllPrincipals();
        if (principals != null) {
            List<SessionInformation> sessions = new ArrayList<>();
            for (Object principal : principals) {
                sessions.addAll(sessionRegistry.getAllSessions(principal, false));
            }
            return sessions.size();
        }
        return 0;
    }
@Autowired
MySessionRegistry sessionRegistry;
身份验证(身份验证、HttpServletRequest请求、HttpServletResponse响应)上的公共无效{
if(calculateMaxSessions(会话注册表)>最大用户数){
抛出新的SessionAuthenticationException(“超出最大用户数”);
}否则{
//鉴定
}
}
public int calculateMaxSessions(会话注册表会话注册表){
最终列表主体=sessionRegistry.getAllPrinciples();
if(主体!=null){
列表会话=新建ArrayList();
for(对象主体:主体){
sessions.addAll(sessionRegistry.getAllSessions(主体,false));
}
return sessions.size();
}
返回0;
}

我希望这有帮助。干杯

非常感谢您的回复。1-所以我只需要创建这个空类2-然后在我的
SecurityConfiguration.java
calss上添加我新创建的类的新实例。我不知道自动连线到底是如何工作的。然后添加此新属性:
sessionregistry(myNewlyCreatedClassInstance)
3-我不知道在哪里进行登录/身份验证(这是我第一次看这个spring的东西。我把它放在哪里?再次感谢你的回答。1-所以我只需要创建这个空类2-然后在我的
SecurityConfiguration.java
calss上添加我新创建的类的一个新实例。我不知道Autowired是如何工作的。然后添加这个新属性。):
SessionRegistry(myNewlyCreatedClassInstance)
3-我不知道在哪里进行登录/身份验证……:(这是我第一次看这个spring的东西。我应该把它放在哪里?再次感谢