0

I am new to Spring. Client requests to access the rest services, giving username and password in http login provided by Spring, as shown in the image. I don't want to save credentials('username and password') in the application.properties. When I provide the credentials and enter login button, I need to read data (in my case username-'root' and pwd-'root') provided by the user from login and use it in my algorithm, do the process and then authenticate. Is there a way?

Please help me, if anyone has any idea.

authentication login screen Here is my sample code:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private MyBasicAuthenticationEntryPoint authEntryPoint;

@Autowired
private MyUserDetailsService userDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    // auth.inMemoryAuthentication().withUser("user1").password("user1Pass").roles("ADMIN")
    // .and().withUser("user2").password("user2Pass").roles("ADMIN");

    auth.authenticationProvider(authenticationProvider());

}

public DaoAuthenticationProvider authenticationProvider() {
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setUserDetailsService(userDetailsService);
    // provider.setPasswordEncoder(new BCryptPasswordEncoder());
    return provider;

}

// I am trying like this but not sure is this the right way
public void details(User user) {
    String name = user.getName();
    String password = user.getPassword();
    System.out.println("name " + name + "password" + password);
}
// I am trying like this but not sure is this the right way
public void userDetails(UsernamePasswordAuthenticationFilter filter) {
    String usernameParameter = filter.getUsernameParameter();
    System.out.println(usernameParameter);
    String passwordParameter = filter.getPasswordParameter();
    System.out.println(passwordParameter);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();

    http.authorizeRequests().anyRequest().authenticated();

    http.httpBasic().authenticationEntryPoint(authEntryPoint);

}

}


@Component
public class MyBasicAuthenticationEntryPoint extends 
BasicAuthenticationEntryPoint{

@Override
public void commence(HttpServletRequest request, HttpServletResponse 
response,
        AuthenticationException authException) throws IOException, 
ServletException {

    response.addHeader("WWW-Authenticate", "Basic realm="  +  
    getRealmName());
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    PrintWriter printWriter= response.getWriter();
    printWriter.println("Http Status 401-" + authException.getMessage());
}

  @Override
  public void afterPropertiesSet() throws Exception {
    //RealName appears in the login window
    setRealmName("Rashmi");
    super.afterPropertiesSet();
  }
}



@SpringBootApplication
public class SpringRestfulWebServiceApplication extends 
SpringBootServletInitializer {

    @Autowired
    CustomerDetailsController customerDetailsController;

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


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder 
    application) {
      return application.sources(SpringRestfulWebServiceApplication.class);
    }

}
R12
  • 23
  • 1
  • 8
  • http://www.baeldung.com/spring-security-authentication-with-a-database – Marc Tarin Jan 30 '18 at 12:52
  • Thanks for the link :) I want to get the username and password entered by the user. Is this possible? – R12 Jan 30 '18 at 13:43
  • Possible duplicate of [How to get the user entered values of username and password in a spring security authenticated login](https://stackoverflow.com/questions/34931606/how-to-get-the-user-entered-values-of-username-and-password-in-a-spring-security) – Marc Tarin Jan 30 '18 at 13:50
  • I had a look on this but this is not what i need. I will try to explain it better Give me few minutes. – R12 Jan 30 '18 at 14:01
  • @MarcTarin it is not duplicate as far I understand. I am looking for different solution. I have edited my question. – R12 Jan 30 '18 at 15:11
  • @R12 Can post your sample code how trying to access the user name password? – Dipak Thoke Jan 30 '18 at 16:05
  • @DipakThoke I have posted my sample code. when the user enters login button, the credentials entered in the username and password textfield on the authentication login popup provided by spring security need to retrieved. I am completely new to Spring not able to find a proper solution. – R12 Jan 31 '18 at 09:56
  • You have login method right? – Dipak Thoke Jan 31 '18 at 12:28
  • In your login contorller – Dipak Thoke Jan 31 '18 at 12:28
  • I don't have login controller, because just by including dependency will get a login popup from spring. – R12 Jan 31 '18 at 12:39

0 Answers0