I have a java spring boot application with some web services and to access them you need to login first (the login is made with spring security). I also want to create an android app to login there and access my services.
I know that for a web app I just need to add a login.html
file in src/main/webapp
folder of my project and to notify about that in configure(HttpSecurity http)
method overridden from WebSecurityConfigurerAdapter
class.
How can I create the login in my android app and how to notify spring-security from my server application about that?
Here is my spring-security configuration class
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/userPage").hasAuthority("USER")
.antMatchers("/adminPage").hasAuthority("ADMIN")
.antMatchers("/home").permitAll()
.and()
.formLogin()
.and()
.httpBasic()
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(encoder());
}
}
You will have to make an API call from your mobile app on Login action. I have done the same.I had a spring boot REST service that would authenticate the user ad return a sessionID to be uses throughout that login. You can check the below example.
void login(username,password,withSession){
try {
bb.put("outputSchema", "VIEW_DATA");
parameters.put("userId", username);
parameters.put("password", password);
parameters.put("filter", withSession);
} catch (JSONException e) {
e.printStackTrace();
}
String URL ="http://localhost:8080/login"+userName ;
RequestQueue requestQueue = Volley.newRequestQueue(this);
final JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST,
URL,
parameters,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
progressDialog.dismiss();
try {
res=rps.parse(response);
Toast.makeText(MainActivity.this,"Connected",Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(LoginActivity.this,"Connection timed out",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this,"Not able to
connect",Toast.LENGTH_SHORT).show();
}
});
}
Or you can check this for further explanation:- https://www.simplifiedcoding.net/android-volley-tutorial/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.