【问题标题】:Spring boot + Oauth2 + JwtSpring Boot + Oauth2 + Jwt
【发布时间】:2020-10-25 12:45:27
【问题描述】:

我正在尝试创建一个 Spring Boot 应用程序,其中管理员需要使用表单登录 [Default spring form login] 进行不同的登录,客户需要通过 Angular 应用程序登录。客户可以在本地和社交 [Google] 上登录。谁能给我推荐一份文件。

我不需要sso,因为我不会包含第三方登录。

我上网两天了,没找到解决办法。

【问题讨论】:

  • 为了理解你的问题,让我们简化一下。假设您有一组用户的登录 url 和登录页面,以及其他用户的另一个登录 url 和不同的登录页面。因此,当未经身份验证的用户尝试访问受保护的 url 时,系统需要重定向到登录页面。由于是未经身份验证的用户,系统不知道是重定向到第一个登录页面还是第二个登录页面。因此,如果您能告诉我您计划如何合理地解决此问题,那么我可能会指出一些实现或文档
  • @KavithakaranKanapathippillai,感谢您的帮助,实际上我有一个具有不同用户角色 [管理员,用户] 的数据库。 Spring Boot 在此应用程序中充当后端。 Angular 用于前端部分。与该管理面板一起集成在 using thymleaf 中。因此,在我的情况下,用户应该能够使用用户名和密码或社交登录 [JWT 和 Oauth2] 从 Angular 应用程序登录。对于adminpanel,我们需要一个单独的表单登录[Basic spring login form]。你能帮我解决这个问题吗?请给我建议一种方法。提前致谢
  • 那么对于 Angular 应用来说,登录屏幕会不会是这样的stackoverflow.com/users/…(在隐身寡妇中打开)?
  • @KavithakaranKanapathippillai 是的,如果我们转到domain.com/admin/login,我们将获得另一个基本表单登录。这是一个管理员登录页面。管理员不需要角度应用程序。只允许用户。
  • 告诉我进展如何。如果你设法让这个工作,我可以帮助你面板

标签: java spring-boot spring-security oauth-2.0 jwt


【解决方案1】:

以下将允许您通过表单登录或 Github Oauth2 登录使用登录

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/login/authenticate")
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/logout")
                .deleteCookies("JSESSIONID")
                .and().
            oauth2Login().
                loginPage("/login");;
    }

    @Bean
    @Override
    public UserDetailsService userDetailsService() {
        UserDetails user =
             User.withDefaultPasswordEncoder()
                .username("user")
                .password("password")
                .roles("USER")
                .build();

        return new InMemoryUserDetailsManager(user);
    }
}
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");
    }

}
@SpringBootApplication
public class SecuringWebApplication {

    public static void main(String[] args) throws Throwable {
        SpringApplication.run(SecuringWebApplication.class, args);
    }

}

src/main/resources/templates/login.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example </title>
    </head>
    <body>
        <div th:if="${param.error}">
            Invalid username and password.
        </div>
        <div th:if="${param.logout}">
            You have been logged out.
        </div>
        <form th:action="@{/login/authenticate}" method="post">
            <div><label> User Name : <input type="text" name="username"/> </label></div>
            <div><label> Password: <input type="password" name="password"/> </label></div>
            <div><input type="submit" value="Sign In"/></div>
        </form>
        <a href="/oauth2/authorization/github">Click here to login via Github</a>
    </body>
</html>

src/main/resources/templates/hello.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>This is a secured page. Hello World!</title>
    </head>
    <body>
        
        <form th:action="@{/logout}" method="post">
            <input type="submit" value="Sign Out"/>
        </form>
    </body>
</html>

src/main/resources/templates/home.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:th="https://www.thymeleaf.org" 
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Security Example</title>
    </head>
    <body>
        <h1>Welcome! This is not a secured page</h1>
        
        <p>Click <a th:href="@{/hello}">here</a> to see a secured page</p>
    </body>
</html>

pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-oauth2-client</artifactId>
        </dependency>

application.yaml

spring:
  security:
    oauth2:
      client:
        registration:
          github:
            client-id: your-github-client-id
            client-secret: your-github-client-secret

注意:

您必须按照以下参考中的说明生成 github 客户端和密码。

https://spring.io/guides/tutorials/spring-boot-oauth2/#github-register-application

这些是通过上述配置链接的过滤器,请随意在过滤器 5、6、7、12 和 13 中设置断点

  • 尝试访问不安全的 URL /home 看看会发生什么
  • 尝试访问安全 URL /hello 看看会发生什么
  • 尝试访问login url 看看会发生什么
  • 尝试从 login 提交,看看会发生什么
  • 点击login via Github链接看看会发生什么

【讨论】:

    猜你喜欢
    • 2020-06-19
    • 2023-03-18
    • 2019-02-10
    • 2017-01-31
    • 2018-01-05
    • 2022-12-02
    • 2018-10-29
    • 2018-12-22
    • 2020-04-23
    相关资源
    最近更新 更多