Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 security的spring boot secure rest api_Java_Spring_Spring Boot_Spring Security - Fatal编程技术网

Java 带spring security的spring boot secure rest api

Java 带spring security的spring boot secure rest api,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,我有一个web应用程序,我想保护我的restapi。我遵循一些教程,成功地实现了restapi的安全性。 但是当我第一次调用这些页面时,当我在我的web应用程序中添加html文件时,它会显示要进入的登录区域。 我只是想保护RESTAPI,而不是所有的web应用程序 在我的例子中,这是我的application.properties spring.datasource.url=jdbc:mysql://localhost/geekycoders_myteam spring.datasource.u

我有一个web应用程序,我想保护我的restapi。我遵循一些教程,成功地实现了restapi的安全性。 但是当我第一次调用这些页面时,当我在我的web应用程序中添加html文件时,它会显示要进入的登录区域。 我只是想保护RESTAPI,而不是所有的web应用程序 在我的例子中,这是我的application.properties

spring.datasource.url=jdbc:mysql://localhost/geekycoders_myteam
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
logging.level.org.springframework.boot.autoconfigure.security=INFO
security.user.name=admin
security.user.password=admin
这是我的SecurityConfig类

这是一个示例控制器

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    UserRepository userRepository;

    @RequestMapping("/findall")
    @ResponseBody
    public List<User> findall(){
        return userRepository.findAll();

    }
    @RequestMapping("/find")
    @ResponseBody
    public User getUser(@PathParam("id") int id){
        return userRepository.findOne(id);
    }

}

我在目录webapp中添加了index.html一些帮助

代码没有问题。您还需要设置一个默认配置,该配置将允许其他所有操作。XML等效安全表达式可以如下所示确保按正确的顺序排列:

 <security:http use-expressions="true" pattern="/**">
    <security:intercept-url pattern="/**" access="permitAll()"/>
 </security:http>

您可以允许访问某些目录:

http.authorizeRequests().antMatchers("/css/**", "/js/**", "/images/**").permitAll();

如果有人现在正在阅读本文,我注意到,通过在构建文件中包含Spring安全性,默认情况下整个应用程序都会启用授权。

我需要允许访问我的html文件而不提示登录。您可以将/html/**添加到列表中,以访问该目录下的所有文件。它们是路径,在我的例子中,我将文件放在不同的目录中。如果文件位于根目录下,只需添加/**。请在.antmatchersttpmethod.GET、/api/**.authenticated.antmatchersttpmethod.GET、/**.permitAll之后尝试这些代码行
http.authorizeRequests().antMatchers("/css/**", "/js/**", "/images/**").permitAll();