Junior — Middle
What is your experience in implementing user authorization and authentication systems?
sobes.tech AI
Answer from AI
In Java projects, I implemented authentication and authorization systems using Spring Security. I usually used JWT (JSON Web Tokens) for user authentication, which allows securely transmitting user information between the client and server.
Here's an example of a simple Spring Security configuration class:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/login", "/register").permitAll()
.anyRequest().authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// Adding a filter to check JWT
http.addFilterBefore(jwtFilter(), UsernamePasswordAuthenticationFilter.class);
}
// Other methods for authentication and token generation
}
I also stored users in a database with password hashing (for example, using BCryptPasswordEncoder). It's important to distinguish between authentication (identity verification) and authorization (access rights verification).