简体   繁体   English

Spring 引导 jwt 仅认证

[英]Spring boot jwt authentication only

I'm trying to implement spring boot authentication without any authorization.我试图在没有任何授权的情况下实施 spring 引导身份验证。 So simple signin,signup and profile apis where the signin and signup api has been permitted for all and profile is only for authenticated users.如此简单的登录、注册和个人资料 api,其中允许所有人登录和注册 api,而个人资料仅适用于经过身份验证的用户。

Most tutorials focus on RBAC and authentication together while I just want to focus on the authentication part.大多数教程同时关注 RBAC 和身份验证,而我只想关注身份验证部分。

I have already created the basic structure with the help of scattered tutorials.我已经在分散的教程的帮助下创建了基本结构。

My AuthController:我的授权控制器:

    private final AuthService authService;

    @PostMapping("/signin")
    public ResponseEntity<?> signin(@RequestBody SigninRequest signinRequest) {
        String email = signinRequest.getEmail();
        log.info("email : {}", email);
        String password = signinRequest.getPassword();
        User user = authService.signin(email, password);
        return ResponseEntity.ok().body(user);
    }

    @PostMapping("/signup")
    public ResponseEntity<User> signup(@RequestBody User user){
        URI uri = URI.create(ServletUriComponentsBuilder.fromCurrentContextPath().path("/api/v1/auth/signup").toUriString());
        return ResponseEntity.created(uri).body(authService.signup(user));
    }

My AuthServiceImpl:我的 AuthServiceImpl:

public class AuthServiceImpl implements AuthService, UserDetailsService {
    private final AuthRepository authRepository;
    private final PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        User user = authRepository.findByEmail(email);
        if (user == null) {
            throw new UsernameNotFoundException(email);
        } else {
            log.info("User found: {}", user);
        }
        return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), new ArrayList<>());
    }

    @Override
    public User signup(User user) {
        String encodedPassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodedPassword);
        return authRepository.save(user);
    }

    @Override
    public User signin(String email, String password) {
        // handle login ??
        return user;
    }
}

Everything was going fine until every tutorial hits the point where they send authorities or roles back to the client side.一切都很顺利,直到每个教程都达到将权限或角色发送回客户端的程度。 I wish to make this application authentication only.我只想进行此应用程序身份验证。

And my security config as of now:到目前为止,我的安全配置是:

public class SecurityConfig extends WebSecurityConfigurerAdapter {
    private final UserDetailsService userDetailsService;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.authorizeRequests()
                .antMatchers("/api/v1/auth/signin").permitAll()
                .antMatchers("/api/v1/auth/signup").permitAll()
                .antMatchers("/api/v1/auth/profile").authenticated();
        http.httpBasic();
    }
}

My plan is simple:我的计划很简单:

  1. User can register用户可以注册
  2. User Can login用户可以登录
  3. Upon login a access token and refresh token will be issued like usual登录后,将像往常一样发出访问令牌和刷新令牌

A Dockerised role based access system and advanced modifiable user-api access system基于 Dockerised 角色的访问系统和高级可修改用户 API 访问系统

Spring Security, JWT, DB Based Authentication upon APIs Spring 安全性,JWT,基于 API 的数据库身份验证

https://github.com/abhitkumardas/rbac https://github.com/abhitkumardas/rbac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM