I am new to spring security and I am trying to build a restful API based sample Spring Security based example and I have used Spring Boot to create project.But I am getting following error when I send a request url:-

Request Url:-

http://localhost:8080/message

Response Got: -

{"timestamp":1505139451257,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/message"}

The Below is the code I have added:-

1] Main Class

@SpringBootApplication
@ComponentScan("com.srikss.controller")
public class SrikSsApplication {

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

Controller:-

@RestController
public class HelloWorldController {

    @RequestMapping(value="/message",method=RequestMethod.GET,headers="Accept=application/json")
    public String messageLoad()
    {
        return "hello";
    }

}

Configuration Class: -

@Configuration
@EnableWebSecurity
public class SrikSSConfiguration extends WebSecurityConfigurerAdapter{



    @Override
    protected void configure(HttpSecurity httpsecurity) throws Exception
    {
        httpsecurity.authorizeRequests().anyRequest().permitAll().and()
        .httpBasic();
        httpsecurity.csrf().disable();
    }

}

Can anyone help me to figure out what I am doing wrong here.

Thanks in advance.

1

There are 1 best solutions below

0
On

Finally got the reason what I was doing wrong. with the help of this reference link:- https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html

actually spring boot defines default password which generated randomly and the default user name is "user". After I update the password in application.properties file got the desired result,

security.user.password=aaa