spring boot保持自动生成密码和自己的登录页面。我不明白为什么它不能在我的系统中运行,也不能在数据库中使用我的用户名和密码。我曾尝试排除UserDetailsServiceAutoConfiguration.class,但是它也不起作用。
SecurityConfig.java
package com.example.madclassproj;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class RemoteContent extends AsyncTask<String, Void, String> {
private String ResponseStatus;
private String ResponseMessage;
private String TablesInfo;
private String ProductsInfo;
private Context CallingContext;
DatabaseHelper myDb;
RemoteContent(Context ct) {
super();
CallingContext = ct;
}
private static final String TAG = "RemoteContent";
/// Background download of the page.
@Override
protected String doInBackground(String... urls) {
String response = "";
try {
URL url = new URL(urls[0]);
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
response = readStream(in);
} finally {
urlConnection.disconnect();
}
} catch (java.io.IOException ioex) {
System.out.println("Exception Catched: Java IO");
}
} catch (MalformedURLException muex) {
System.out.println("Exception Catched : Malformed URL");
}
return response;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Determine the application's actions according to the data sent by the control tower ///////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
protected void onPostExecute(String result) {
String opentag1 = "<response>";
String closetag1 = "</status>";
String statustag = "<status>";
int p1 = result.indexOf(opentag1) + opentag1.length()+ statustag.length()+2;
int p2 = result.indexOf(closetag1);
String ResponseStatus = result.substring(p1, p2);
if (ResponseStatus.equals("0-FAIL")) {
Intent intent = new Intent(CallingContext, LoginActivity.class);
CallingContext.startActivity(intent);
} else if (ResponseStatus.equals("3-OK")){
ResponseMessage = getXMLContent(result, "<msg>", "</msg>", 0);
TablesInfo = getXMLContent(result, "<tables>", "</tables>", 0);
ProductsInfo = getXMLContent(result, "<products>", "</products>", 0);
Log.d(TAG, TablesInfo);
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(TablesInfo));
String tag = "";
String text = "";
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
tag = xpp.getName();
switch (eventType){
case XmlPullParser.START_TAG:
if(tag.equalsIgnoreCase("id")){
text = xpp.getText();
Log.d(TAG, text);
}else if (tag.equalsIgnoreCase("status")){
Log.d(TAG, text);
}
break;
}
eventType = xpp.next();
}
}catch (XmlPullParserException | IOException e){
e.printStackTrace();
}
}
}
private String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
private String getXMLContent(String str, String XMLOpen, String XMLClose, int start) {
String content = "";
/// Get the message
int p1 = str.indexOf(XMLOpen, start);
int p2 = str.indexOf(XMLClose, p1);
if (p1 > 0 && p2 > p1) {
p1 += XMLOpen.length();
content = str.substring(p1, p2);
}
return content;
}
public String getResponseStatus() { return ResponseStatus; }
public String getTablesInfo() { return ResponseMessage; }
}
}
UserDetailServiceImpl
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bcCryptPasswordEncoder = new BCryptPasswordEncoder();
return bcCryptPasswordEncoder;
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().antMatchers("/", "/login", "/logout").permitAll();
http.authorizeRequests().antMatchers("/userInfo").access("hasAnyRole('user', 'admin')");
http.authorizeRequests().and().formLogin()//
// Submit URL của trang login
.loginProcessingUrl("/j_spring_security_check") // Submit URL
.loginPage("/login")//
.defaultSuccessUrl("/userInfo")//
.failureUrl("/login?error=true")//
.usernameParameter("username")//
.passwordParameter("password")
// Cấu hình cho Logout Page.
.and().logout().logoutUrl("/logout").logoutSuccessUrl("/logoutSuccessful");
}