Java 在spring boot应用程序中阻止通过url栏访问其他网页

Java 在spring boot应用程序中阻止通过url栏访问其他网页,java,spring-boot,spring-security,Java,Spring Boot,Spring Security,我在spring启动应用程序方面有一个基本问题。开始网页为,登录后用户重定向到。不幸的是,也有可能在url栏中写入内容,然后在没有身份验证的情况下访问。 阻止这一行动的主要想法是什么 @编辑 这是我的配置: package local.vlex.security; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.

我在spring启动应用程序方面有一个基本问题。开始网页为,登录后用户重定向到。不幸的是,也有可能在url栏中写入内容,然后在没有身份验证的情况下访问。 阻止这一行动的主要想法是什么

@编辑 这是我的配置:

 package local.vlex.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "src/main/resources/static/assets/css/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }
}
和控制器:

@GetMapping("/MainMenu")
    public ModelAndView correctCredentials(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("MainMenu");
        return mav;
    }
总结:
我想在没有身份验证的情况下阻止访问其他站点-如果有人没有登录,那么除了/login之外,它应该在所有站点上执行404。添加以下代码,它将阻止除“/”&“/login”之外的所有请求

并添加WebConfig

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry
                .addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(31556926)
                .resourceChain(true);
    }
}
不要忘记在pom.xml文件中添加安全依赖项

 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
 </dependency>

org.springframework.boot
弹簧启动安全

添加以下代码,它将阻止除“/”和“/登录”之外的所有请求

并添加WebConfig

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry
                .addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(31556926)
                .resourceChain(true);
    }
}
不要忘记在pom.xml文件中添加安全依赖项

 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
 </dependency>

org.springframework.boot
弹簧启动安全

如果您在类路径上有spring安全依赖项,这是我的代码中的一个工作示例

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/", "/login**")
                .permitAll()
            .anyRequest()
                .authenticated();
这会阻止我通过授权请求“/”或“登录”或“登录页面”等以外的任何其他请求

然后忽略静态的,css,js等等

如果这仍然不起作用,现在请尝试单独阻止页面:

http.authorizeRequests()
                .antMatchers("/MainMenu")
                .authenticated();

以上将给出发生了什么的想法。依赖关系问题或类似问题。

如果您在类路径上有spring安全依赖关系,这是我的代码中的一个工作示例

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/", "/login**")
                .permitAll()
            .anyRequest()
                .authenticated();
这会阻止我通过授权请求“/”或“登录”或“登录页面”等以外的任何其他请求

然后忽略静态的,css,js等等

如果这仍然不起作用,现在请尝试单独阻止页面:

http.authorizeRequests()
                .antMatchers("/MainMenu")
                .authenticated();
以上将给出发生了什么的想法。依赖性问题或类似的问题

如果有人没有登录,那么它应该在所有其他方面都转到404 超过/登录

这是一个有点特殊的要求。通常情况下,人们会被重定向到除登录以外的所有其他URL上的登录页面,或者被
401未经授权
403禁止
。然而,这也不难做到

您提到您还需要资产目录中的静态资源(css、js、图像),因此,考虑到这一点,这应该可以工作:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.HttpStatusEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
@EnableWebSecurity
public class YourSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/login", "/assets/**").permitAll()
                .anyRequest().authenticated()
            .and()
                .exceptionHandling()
                .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.NOT_FOUND))
                ;
    }
}
使用

如果有人没有登录,那么它应该在所有其他方面都转到404 超过/登录

这是一个有点特殊的要求。通常情况下,人们会被重定向到除登录以外的所有其他URL上的登录页面,或者被
401未经授权
403禁止
。然而,这也不难做到

您提到您还需要资产目录中的静态资源(css、js、图像),因此,考虑到这一点,这应该可以工作:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.HttpStatusEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
@EnableWebSecurity
public class YourSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/login", "/assets/**").permitAll()
                .anyRequest().authenticated()
            .and()
                .exceptionHandling()
                .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.NOT_FOUND))
                ;
    }
}
使用



至少向我们显示您的安全配置。
antMatchers(“/MainMenu”).permitAll()
。您特别允许所有请求进入主菜单。此外,如果Maruf的答案不符合您的要求,您应该指定它们。我们无法猜测您想要实现什么。@dunni您能看一下已编辑的代码吗?现在只允许“/”,但它将重定向到“/登录”。不幸的是,现在我可以从url访问任何网站。如果这不是你想要的,告诉我们你想要什么。是否允许使用特定页面?是否只想阻止主菜单?我想阻止所有未登录的网站。如果有人没有登录,那么我不想让他先进入/main菜单、/ProductPage、/ChangePassword等等。他应该先进入/login,只有通过身份验证,才能进入上述其他网站。至少向我们展示您的安全配置。
antMatchers(/main菜单”).permitAll()
。您特别允许所有请求进入主菜单。此外,如果Maruf的答案不符合您的要求,您应该指定它们。我们无法猜测您想要实现什么。@dunni您能看一下已编辑的代码吗?现在只允许“/”,但它将重定向到“/登录”。不幸的是,现在我可以从url访问任何网站。如果这不是你想要的,告诉我们你想要什么。是否允许使用特定页面?是否只想阻止主菜单?我想阻止所有未登录的网站。如果有人没有登录,那么我不想让他先进入/main menu、/ProductPage、/ChangePassword等等。他应该先进入/login,只有通过身份验证,然后才能进入上述其他网站。好吧,现在无论我在之后键入什么,都要进入。在HomeController中,我有:@GetMapping(“/”)公共字符串startHtml(){return“redirect:/login”}它也丢失了css,所以我不得不添加:.antMatchers(“/”,“sec/main/resources/static/assets/css/**”).permitAll()和@ComponentScan(basePackageClasses=HomeController.class)但它破坏了sping的安全性——同样,所有网页都可以在url栏中访问,但仍然可以通过在url栏中键入地址访问其他网站/编辑我的答案,请检查@michu93仍然无法工作,我重新启动了应用程序,在url栏中键入内容,然后毫无问题地去了那里;/@Maruf Hassanwell,现在无论我在后面键入什么,它都会转到。在HomeController中,我有:@GetMapping(“/”)公共字符串startHtml(){return“redirect:/login”}它也丢失了css,所以我不得不添加:.antMatchers(“/”,“sec/main/resources/static/assets/css/**”).permitAll()和@ComponentScan(basePackageClasses=HomeController.class)但它破坏了sping的安全性——同样,所有网页都可以在url栏中访问,但仍然可以通过在url栏中键入地址访问其他网站/编辑我的答案,请检查@michu93仍然无法工作,我重新启动了应用程序,在url栏中键入内容,然后毫无问题地去了那里;/@Maruf HassanIt和我的差不多,但有两个问题。首先,它会释放css、图像、js文件,其次,当我添加了阻塞时,它不会转到404