【问题标题】:Disable authentication for some methods禁用某些方法的身份验证
【发布时间】:2016-12-07 16:25:13
【问题描述】:

当我包含以下依赖项时,Spring 要求对所有休息请求进行身份验证。

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

我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>MyApp</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

我的 UserController.java

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    UserService us;

    @RequestMapping("/all")
    public Hashtable<String, User> getAll() {
        return us.getAll();
    }

    @RequestMapping("{id}")
    public User getPerson(@PathVariable("id") String id) {
        return us.getPerson(id);
    }

    @RequestMapping(method = RequestMethod.POST)
    public void registerNewUser() {
        us.registerNewUser();
    }
}

如何禁用registerNewUser() 方法的身份验证?它用于注册用户,我不希望对该方法进行任何身份验证。

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    您需要一个安全配置类,并且可以从那里明确限制该行为。例如:

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
            .authorizeRequests()
              .antMatchers("/users/**").authenticated()
              .antMatchers(HttpMethod.POST, "/users").permitAll();  
      }
    }
    

    【讨论】:

    • 我在 org.springframework.security.samples.config 包下创建了这个类,但在响应正文中我仍然得到 Unauthorized
    • 您能否确保配置已被您的应用程序正确扫描并连接?它应该被记录下来。
    • Spring boot 启动时日志中没有提到“SecurityConfig”。我还尝试在配置方法上方添加@Autowired
    猜你喜欢
    • 1970-01-01
    • 2016-09-26
    • 2014-03-31
    • 2020-03-21
    • 1970-01-01
    • 2020-10-29
    • 2023-03-15
    • 2010-10-24
    • 1970-01-01
    相关资源
    最近更新 更多