Spring-security blocking anonymous users from pulling data from MongoDB - mongodb

I have a Spring Boot web application that uses Spring Security. The index.html page contains a method call (POST) to the controller that loads objects from MongoDB into a ArrayList and returns it so it can be displayed on the front page.
It seems like Spring Security is preventing POST requests for anonymous users. If I first login so the "/loadContent" method is called, and thereby log out, everything works well. I do pass the CSRF tokens before calling the method.
My "WebSecurityConfig":
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/loadContent")
.permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.exceptionHandling()
.accessDeniedPage("/403");
}

CSRF is enabled by default in spring security.
A possible solution is to disable it manually (see last line in code below).
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/loadContent")
.permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.exceptionHandling()
.accessDeniedPage("/403")
.and().csrf().disable();
}
Update:
If you want to use csrf, which I encourage, maybe think about securing an additional REST endpoint e.g. starting with /api/.
In the example below these endpoints are secured using Basic Authorization with a user called api, but you can easily change it to allow anonymous users to request to resources:
#Configuration
#EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("api").password("api").roles("API").and()
.withUser("user").password("user").roles("USER").and()
.withUser("admin").password("admin").roles("USER", "API", "ADMIN");
}
#Configuration
#Order(1) // higher order = lower priority
public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
#Override
protected void configure(HttpSecurity http) throws Exception {
// no csrf when communicating directly with the backend api
http
.antMatcher("/api/**")
.authorizeRequests()
.anyRequest().hasAnyRole("API")
.and()
.httpBasic()
.and()
.csrf().disable();
http.sessionManagement().disable();
}
}
#Configuration
#Order(2) // higher order = lower priority
public static class UIWebSecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/**").hasAnyRole("USER", "ADMIN").anyRequest().authenticated();
http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
http.httpBasic().disable();
}
}
}

Turned to be a typo in the #RequestMapping area of the controller. Thank you very much for your assistance.

Related

How to whitelist single endpoint in Spring Boot application?

I am new to Spring Boot and trying to find out the way to whitelist an
end-point. I have enabled the Spring Security.
I have a controller class with endpoint Hello, which should return "hello"
in response and want anyone to be able to access this endpoint without authentication required.
#RestController
#RequestMapping(value = {"/employee"})
public class EmployeeController {
#Autowired
EmployeeRepository empRepose;
#Autowired
EmployeeService empService;
#Autowired
private Utility utility;
#PreAuthorize("permitAll()")
#GetMapping(value = "/hello", produces = MediaType.APPLICATION_JSON_VALUE)
public String home() {
return "Hello Employee!";
}
}
Spring Security configuration:
#Configuration
#EnableGlobalMethodSecurity(prePostEnabled = true)
#EnableWebSecurity
public class ApplicationBasicAuth extends WebSecurityConfigurerAdapter {
#Autowired
RegisterUser beanRegisteruser;
#Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
/* httpSecurity.csrf().disable()
.authorizeRequests().anyRequest().authenticated()
.and().httpBasic();*/
/*httpSecurity
.httpBasic()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/employee/**").permitAll()
.and()
.csrf().disable();*/
httpSecurity.csrf().disable();
httpSecurity.authorizeRequests().anyRequest().permitAll();
}
I tried so many ways to whitelist all endpoints or even 1 endpoint for which I don't need to go for authentication.
Please, help me to find out what I am doing wrong here.
You can achieve using configure(WebSecurity web) and/or configure(HttpSecurity http) If you are using both of them note that you have to keep configure(WebSecurity web) above configure(HttpSecurity http). You may see more details here
configure(WebSecurity web)
General use of WebSecurity ignoring() method omits Spring Security and none of Spring Security’s features will be available.
#Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/hello")
}
configure(HttpSecurity http)
You can also use configure(HttpSecurity http) method with .permitAll() as below
#Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/hello").permitAll()
.anyRequest().authenticated();
}

Spring Boot security for rest

I know there are a lot of topics on that but is there any way just modify the normal spring security to work with json objects.
#Configuration
#EnableWebSecurity
#EnableGlobalMethodSecurity(prePostEnabled = true) //za pre i post authorize v servisa
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter
{
//Koi service shte polzvame
#Autowired
private UserService userService;
#Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.antMatchers("/", "/user/register", "/css/**", "/js/**").permitAll()
.antMatchers("/user/user").access("hasRole('USER') or hasRole('ADMIN')")
.antMatchers("/user/admin").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/user/login").permitAll()
.usernameParameter("username")
.passwordParameter("password")
.and()
.rememberMe().rememberMeCookieName("RememberMeFromLecture")
.rememberMeParameter("remember")
.key("golqmaTaina")
.and()
.logout().logoutSuccessUrl("/user/login?logout").logoutRequestMatcher(new AntPathRequestMatcher("/signout")).permitAll()
.and()
.exceptionHandling().accessDeniedPage("/user/unauthorized")
.and().csrf().disable();
}
#Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.userDetailsService(this.userService).passwordEncoder(getBCryptPasswordEncoder());
}
#Bean
public BCryptPasswordEncoder getBCryptPasswordEncoder()
{
return new BCryptPasswordEncoder();
}
}
This is my config file and it works perfectly without rest, but my problem is just want to make the login page to work with rest that's all. If it's configed like this, my login is been done automatically I can't even set a break point inside my controllers. It works, but i want to make it work with rest.
I created a sample application (https://github.com/manishsingh27/TokenBasedAuth) and it is based on REST for authentication.
Client application is based on AngularJS and it has login page, files are here - https://github.com/manishsingh27/TokenBasedAuth/tree/main/authz/src/main/resources/static.
And REST APIs are present here - https://github.com/manishsingh27/TokenBasedAuth/blob/main/authz/src/main/java/com/adms/authz/self/user/controller/UsersController.java.
Config file is here -https://github.com/manishsingh27/TokenBasedAuth/blob/main/authz/src/main/java/com/adms/authz/config/SecurityConfiguration.java
You need to use the #EnableResourceServer annotation to secure the Rest APIs.

Spring + Oauth2: how to refresh access token

I am building rest web services with Spring Boot. Authentication is implemented using Spring Security and OAuth2. Users are authenticated against LDAP server. Here is my websecurityconfig
#Configuration
#EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
private RestAuthenticationSuccessHandler authenticationSuccessHandler;
#Autowired
private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
#Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic()
.and()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(
SessionCreationPolicy.STATELESS)
.and()
.exceptionHandling()
.authenticationEntryPoint(restAuthenticationEntryPoint)
.and()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/logout").permitAll()
.antMatchers("/ristore/**").authenticated()
.anyRequest().authenticated()
.and()
.formLogin()
.successHandler(authenticationSuccessHandler)
.failureHandler(new SimpleUrlAuthenticationFailureHandler());
}
#Override
#Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
#Bean
public RestAuthenticationSuccessHandler mySuccessHandler(){
return new RestAuthenticationSuccessHandler();
}
#Bean
public SimpleUrlAuthenticationFailureHandler myFailureHandler(){
return new SimpleUrlAuthenticationFailureHandler();
}
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
DefaultSpringSecurityContextSource contextSource = getSource();
auth
.ldapAuthentication()
.userDnPatterns("cn={0},ou=institution,ou=people")
.groupSearchBase("ou=groups")
.contextSource(contextSource);
}
}
Additional config is done in authserverconfig including clientdetailservice.
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
#Autowired
#Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
#Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer)
throws Exception {
oauthServer.allowFormAuthenticationForClients();
}
#Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(new InMemoryTokenStore())
.authenticationManager(authenticationManager);
}
#Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("ristoreclient")
.scopes("read")
.authorizedGrantTypes("password", "refresh_token", "client_credentials")
.secret("ristoresecret")
.accessTokenValiditySeconds(60);
}
}
It works for initial login. However when I try to get a new access token with refresh token when the old expires, I got the error "UserDetailsService is required". After searching for answers online, I found this post with similar problem: spring-security-oauth2 2.0.7 refresh token UserDetailsService Configuration. Basically the solution there was to create a custom LdapUserDetailsService. Thing is it was set up in xml config instead of java. Besides, it is not clear how and where this class is injected. In this other case, userdetailservice instance is added in auth server endpoint config instead. This article does not provide the implementation of this class.
The idea of having a userdetailservice, in my opinion, is to look up and see if this user is still active before issuing a new access token. What is contradictory is that the request of getting a refresh_token for oauth2 only consists of the following information which does not include username/password.
client_id=clientid
client_secret=clientsecret
refresh_token=1/6BMfW9j53gdGImsiyUH5kU5RsR4zwI9lUVX-tqf8JXQ&
grant_type=refresh_token
OAuth2 for a Spring REST uses Zuul proxy as a middle layer between front end and web api to handle refresh token which makes the configuration more complex. How should I implement a userdetailsservice for oauth2 in Spring Boot and where should I inject it?

Not able to get csrf token with spring security 3.2.7

I am using Spring Security 3.2.7 with spring boot 1.2.3. I am building Rest application and implementing spring security with java config in which CSRF is on by default and I know I can disable it in my overridden configure method with "http.csrf().disable()". But suppose I don't want to disable it, but I need the CSRF token. When i hit the url
localhost:8080/myproject/url
with POST request it gives
{
"timestamp": 1431682924618,
"status": 403,
"error": "Forbidden",
"message": "Expected CSRF token not found. Has your session expired?",
"path": "/myproject/user"
}
So how can i hit the same url with successful result without disabling the csrf.
My SecurityConfig file is:
#Configuration
#EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
#Autowired
private DataSource dataSource;
#Autowired
private CustomUserDetailsService customUserDetailsService;
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// In memory authentication
/*auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");*/
auth.userDetailsService(customUserDetailsService);
}
#Override
protected void configure(HttpSecurity http) throws Exception {
// http.csrf().disable() //Disable csrf security
http
.authorizeRequests()
.antMatchers("/myproject/user/signup").permitAll()
.antMatchers("/myproject/**").hasRole("ADMIN")
.and().httpBasic();
}
}

Spring Security simulataneous usage of multiple PasswordEncoders

I have spring app with simple configuration of Spring Security using bcrypt (default parameters) and the test works fine, however i want to plan that this application gives the ability to the administrator or the user to change password and select authentication parameters to be used such as:
1)bcrypt (BCryptPasswordEncoder)
2)hash function such as sha (StandardPasswordEncoder),
So the question is how to change the following class (or the AuthenticationManagerBuilder specifically ) in order to reflect that some users could have they password stored as a sha hash but other as bcrypt, Taking into account that the database table already have a column that specify what kind of hash is being stored in the password column i.e. bcrypt or sha.
#Configuration
#EnableWebMvcSecurity
#EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
private UserDetailsService iUserDetailsService;
#Override
protected void configure(HttpSecurity http) throws Exception {
http
// .csrf().disable()
// .headers().disable()
.headers()
.contentTypeOptions()
.xssProtection()
.cacheControl()
.httpStrictTransportSecurity()
.frameOptions()
.addHeaderWriter(new StaticHeadersWriter("X-Content-Security-Policy","script-src 'self"))
.addHeaderWriter(new StaticHeadersWriter("Content-Security-Policy","script-src 'self'"))
.addHeaderWriter(new StaticHeadersWriter("X-WebKit-CSP","default-src 'self'"))
.and()
.exceptionHandling()
.accessDeniedHandler(syncAccessDeniedHandler())
.and()
.authorizeRequests()
.antMatchers( "/register",
"/static/**",
"/h2/**",
"/resources/**",
"/static/css/**",
"/static/img/**" ,
"/static/js/**",
"/static/pdf/**",
"/resources/static/css/**",
"/resources/static/img/**" ,
"/resources/static/js/**",
"/resources/static/pdf/**",
"/pdf/**",
"/css/**",
"/js/**",
"/img/**"
).permitAll()
.antMatchers("/admin/dashboard/**").hasAnyRole("STUDENT", "ADMIN")
.antMatchers("/admin/network/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
#Autowired
public void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(iUserDetailsService).passwordEncoder(pwEncoder());
}
#Bean
public BCryptPasswordEncoder pwEncoder() {
return new BCryptPasswordEncoder();
}
#Bean
public SyncAccessDeniedHandler syncAccessDeniedHandler() {
String uri = "/403";
return new SyncAccessDeniedHandler(uri);
}
}
maybe you can use DelegatingPasswordEncoder from spring 5.0 or extend it to create your own version.
https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/crypto/password/DelegatingPasswordEncoder.html