Wednesday, November 19, 2008

Let Java SSL Trust All Certificates without Violating Security Manager

Java SSL by default does not trust self-signed certificate. Wikibooks:Programming reveals a way to allow connection to secure HTTP server using self-signed certificate. The magic looks like:

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
// do nothing
}

public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
// do nothing
}
}
};

// Install the all-trusting trust manager
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
} catch(GeneralSecurityException gse) {
throw new IllegalStateException(gse.getMessage());
}
HttpsURLConnection.setDefaultSSLSocketFactory(
sc.getSocketFactory());

However, HttpsURLConnection.setDefaultSSLSocketFactory(...) will throw a SecurityException (a RuntimeException) if a security manager exists and its checkSetFactory method does not allow a socket factory to be specified. The thrown SecurityException looks like

Exception in thread "main" java.security.AccessControlException: access denied (java.lang.RuntimePermission setFactory)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkSetFactory(SecurityManager.java:1612)
at javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(HttpsURLConnection.java:308)
at SecurityManagerTest.main(SecurityManagerTest.java:50)

A workaround to avoid such a SecurityException is as below:

URL url = new URL("https://engage.ac.uk");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.getInputStream();

The trick is to use the instance method setSSLSocketFactory instead of the static method setDefaultSSLSocketFactory. The former does not throw a SecurityException.

Note: need to use conn.getInputStream() instead of url.openStream(), otherwise the customised SocketFactory won't be used.

Of course to allow to connect the secure web site, the following permission should be added in the Java security policy file:

permission java.net.SocketPermission "engage.ac.uk:443", "connect";

100 comments:

andrea chiu said...

There are certain point in our life that we encounter failure but it doesn't mean you will lose hope and give up everything but it only means that every failure there's an exchange and that is new beginning. Well, thank you for sharing your article and keep on posting. Visit my site too for more information.

triciajoy.com

www.triciajoy.com

Unknown said...

Life is a battle, if you don't know how to defend yourself then you'll end up being a loser.
So, better take any challenges as your stepping stone to become a better person. Have fun,
explore and make a lot of memories.

n8fan.net

www.n8fan.net

Kanye Co Jamila said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Dot Net Training in Chennai . Nowadays Java has tons of job opportunities on various vertical industry.


or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

williambli92982 said...

This is the right weblog for anybody who desires to find out about this topic. You understand a lot its nearly onerous to argue with you (not that I really would need…HaHa). You undoubtedly put a brand new spin on a topic thats been written about for years. Nice stuff, just nice! online casino games

Aliya Manasa said...

Very well written blog and I always love to read blogs like these because they offer very good information to readers with very less amount of words....thanks for sharing your info with us and keep sharing.
python Training in Pune
python Training in Chennai
python Training in Bangalore

Unknown said...

Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
Best Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram

priya said...

Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries. I want to say thanks for great sharing.
Data Science Training in Indira nagar
Data Science training in marathahalli
Data Science Interview questions and answers
Data Science training in btm layout
Data Science Training in BTM Layout
Data science training in bangalore

Deepali M said...
This comment has been removed by the author.
franklinraj said...

Thank you for excellent article.

Please refer below if you are looking for best project center in coimbatore

soft skill training in coimbatore
final year projects in coimbatore
Spoken English Training in coimbatore
final year projects for CSE in coimbatore
final year projects for IT in coimbatore
final year projects for ECE in coimbatore
final year projects for EEE in coimbatore
final year projects for Mechanical in coimbatore
final year projects for Instrumentation in coimbatore

digitalsourabh said...

Graphic designing training in bhopal
Python Coaching in Bhopal
Android Coaching in Bhopal
Machine Learning Course in Bhopal
Digital Marketing Training in Bhopal
IoT Training in Bhopal
Artificial Intelligence Training in Bhopal
SEO Training in Bhopal
Minor Training in Bhopal
Major Training in Bhopal

spot said...

super your blog
andaman tour packages
andaman holiday packages
web development company in chennai
Math word problem solver
laptop service center in chennai
Austin Homes for Sale

Sai Institutions said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us. great doing keep sharing.
cruise ship training and placement in chennai
best visual communication insitute in chennai
Carving classes in Chennai
hotel management institute in chennai
hotel management course in chennai
hotel management College in chennai
Fashion Design Course in Chennai

Priyanka said...

Attend The Python Training in Hyderabad From ExcelR. Practical Python Training Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Hyderabad.
python training in bangalore

Techxinx said...

I am a regular reader of your blog and I find it really informative. for more info contact us
CPCT Coaching in Bhopal
java coaching in bhopal
Autocad classes in bhopal
Catia coaching in bhopal

saketh varma said...

I like viewing web sites which comprehend the price of delivering the excellent useful resource Python classes in pune free of charge. I truly adored reading your posting. Thank you!

Raj Tattapure said...

Thanks a lot for writting such a great article. It's really has lots of insights and valueable informtion.
If you wish to get connected with AI world, we hope the below information will be helpful to you.
Python Training Institute in Pune
Python Interview Questions And Answers For Freshers
Data -Science
ML(Machine Learning) related more information then meet on EmergenTeck Training Institute .
Machine Learning Interview Questions And Answers for Freshers
Thank you.!

Raj Tattapure said...

Thanks a lot for writting such a great article. It's really has lots of insights and valueable informtion.
If you wish to get connected with AI world, we hope the below information will be helpful to you.
Python Training Institute in Pune
Python Interview Questions And Answers For Freshers
Data -Science
ML(Machine Learning) related more information then meet on EmergenTeck Training Institute .
Machine Learning Interview Questions And Answers for Freshers
Thank you.!

ExcelR Raising Excellence said...

Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man, Keep it up. Plaese update for machine learning course bangalore

Arslan Bhatti said...

In this modern era, Data Analytics provides a business analytics course with placement subtle way to analyse the data in a qualitative and quantitative manner to draw logical conclusions. Gone are the days where one has to think about gathering the data and saving the data to form the clusters. For the next few years, it’s all about Data Analytics and it’s techniques to boost the modern era technologies such as Machine learning and Artificial Intelligence.

unknow said...

I really enjoyed your blog Thanks for sharing such an informative post.
https://myseokhazana.com/
https://seosagar.in/
Indian Bookmarking list
Indian Bookmarking list
India Classified Submission List
Indian Classified List
Indian Bookmarking list
Indian Bookmarking list
India Classified Submission List
Indian Classified List

shivam said...

Flying Shift - Packers & Movers in Bhopal

Tech News said...

data science training in bangalore
big data and hadoop training in bangalore

Yourdoorstep said...

Thanks for provide great informatic and looking beautiful blog, really nice required information & the things i never imagined and i would request, wright more blog and blog post like that for us. Thanks you once agianMarriage certificate in delhi
Marriage certificate in ghaziabad
Marriage registration in gurgaon
Marriage registration in noida
special marriage act
Marriage certificate online
Marriage certificate in mumbai
Marriage certificate in faridabad
Marriage certificate in bangalore
Marriage certificate in hyderabad thanks once again to all.

Tech Guy said...

Nice content.
For the best AWS training in bangalore, Visit:
AWS training in bangalore

Tech News said...

Good Article
devops training in bangalore
hadoop training in bangalore
iot training in bangalore
machine learning training in bangalore
uipath training in bangalore

seoexpert said...

Nice Post...I have learn some new information.thanks for sharing.
Data Science Courses in Bangalore | Data Science training | Best Data Science institute in Bangalore

Tech News said...

visit here => DEVOPS TRAINING IN BANGALORE

Priyanka said...

Attend The Data Analytics Courses From ExcelR. Practical Data Analytics Courses Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analytics Courses.
ExcelR Data Analytics Courses

Benish said...

Really nice post. Thank you for sharing amazing information.
Python training in Chennai/Python training in OMR/Python training in Velachery/Python certification training in Chennai/Python training fees in Chennai/Python training with placement in Chennai/Python training in Chennai with Placement/Python course in Chennai/Python Certification course in Chennai/Python online training in Chennai/Python training in Chennai Quora/Best Python Training in Chennai/Best Python training in OMR/Best Python training in Velachery/Best Python course in Chennai

Updated Tech News said...

visit here => Big Data and Hadoop training in bangalore

Updated Tech News said...

For Devops Training in Bangalore Visit: Devops Training in Bangalore

Updated Tech News said...

For IOT Training in Bangalore Visit:
IOT Training in Bangalore

Nandhini said...

Python training in bangalore
Python training in Bangalore
Data science with python training in Bangalore
Angular js training in bangalore
Hadoop training in bangalore
DevOPs training in bangalore
Agile and scrum training in bangalore

Techy said...

Visit for Python training in Bangalore:- Python training in Bangalore

Venkatesh CS said...

Thanks for sharing valuable information.
Digital Marketing training Course in Chennai
digital marketing training institute in Chennai
digital marketing training in Chennai
digital marketing course in Chennai
digital marketing course training in omr
digital marketing certification in omr
digital marketing course training in velachery
digital marketing training center in Chennai
digital marketing courses with placement in Chennai
digital marketing certification in Chennai
digital marketing institute in Chennai
digital marketing certification course in Chennai
digital marketing course training in Chennai
Digital Marketing course in Chennai with placement
digital marketing courses in Chennai

Customer relationship management said...

Its help me to improve my knowledge and skills also.im really satisfied in this session.python training in bangalore

Customer relationship management said...

Very interesting blog Thank you for sharing such a nice and interesting blog and really very helpful article.microsoft azure training in bangalore

Customer relationship management said...

Its really helpful for the users of this site. I am also searching about these type of sites now a days. So your site really helps me for searching the new and great stuff.python training in bangalore

Customer relationship management said...

Very useful and information content has been shared out here, Thanks for sharing it.google cloud platform training in bangalore

Customer relationship management said...

I gathered a lot of information through this article.Every example is easy to undestandable and explaining the logic easily.mulesoft training in bangalore

Customer relationship management said...

These provided information was really so nice,thanks for giving that post and the more skills to develop after refer that post.blue prism training in bangalore

Customer relationship management said...

Your articles really impressed for me,because of all information so nice.oracle apps scm training in bangalore

Customer relationship management said...

inking is very useful thing.you have really helped lots of people who visit blog and provide them use full information.sccm training in bangalore

Customer relationship management said...

Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving.vmware training in bangalore

Customer relationship management said...

Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.aws training in bangalore

Customer relationship management said...

I know that it takes a lot of effort and hard work to write such an informative content like this.data science training in bangalore

Data Driller said...

I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work ExcelR Data Analytics Courses

ek said...

This post is very simple to read and appreciate without leaving any details out. Great work!

Please check ExcelR Data Science Course Pune

Priyanka said...

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
ExcelR Data Analytics Course

meritstep Technology said...

Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
top microservices online training

Unknown said...

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful. I am also a blog writer who took data science training bangalore recently and will start blogs on data science

Priyanka said...

Really impressed! Everything is very open and very clear clarification of issues. It contains truly facts. Your website is very valuable. Thanks for sharing.
ExcelR Data Analytics Course

Excelrsolutions said...

Nice information, valuable and excellent work, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here. data science course

vijay said...

Thanks for your valuable post... The data which you have shared is more informative for us...
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore

ek said...

It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips. Perhaps you could write next articles referring to this article. I want to read more things about it!
Please check ExcelR Data Science Courses

Durai Moorthy said...

This is a great article thanks for sharing this informative information.

aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore

Excelrsolutions said...

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work. data science course fees in Bangalore

Unknown said...

Thanks loads for writting this kind of fantastic article.
It's simply has plenty of insights and valueable informtion.

click here formore info.

ravali said...

Really impressed! Everything is very open and very clear clarification of issues. It contains truly facts. Your website is very valuable. Thanks for sharing.
data analytics course mumbai
data science interview questions

hami said...

Amazing work. Extra-normal manner of taking pictures the details.
Thanks for sharing. Waiting in your destiny updates.
filmyzilla

aj said...
This comment has been removed by the author.
Shopclues Winner List said...

Shopclues winner list 2020 here came up with a list of offers where you can win special shopclues prize by just playing a game & win prizes.
Shopclues lucky draw 2020
Shopclues winner name 2020
Shopclues prize list 2020

GlobalEmployees said...

Read my post here
Java programmer
Java programmer
Java programmer

imexpert said...

Good post! I must say thanks for the information.
data analytics course L
Data analytics Interview Questions

Excelrsolutions said...


I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!! data science courses

Excelrsolutions said...


I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!! data science courses

Rajesh Anbu said...

I got useful information by reading this blogs
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore

Excelrsolutions said...

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck... Thank you!!! data analytics courses

ek said...

I have bookmarked your website because this site contains valuable information in it. I am really happy with articles quality and presentation. Thanks a lot for keeping great stuff. I am very much thankful for this site.
Please check this Data Analytics Course in Pune

Priyanka said...

Attend The Machine Learning courses in Bangalore From ExcelR. Practical Machine Learning courses in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Machine Learning courses in Bangalore.
ExcelR Machine Learning courses in Bangalore
We are located at :
Location 1:
ExcelR - Data Science, Data Analytics Course Training in Bangalore
49, 1st Cross, 27th Main BTM Layout stage 1 Behind Tata Motors Bengaluru, Karnataka 560068
Phone: 096321 56744
Hours: Sunday - Saturday 7AM - 11PM

Location 2:
ExcelR
#49, Ground Floor, 27th Main, Near IQRA International School, opposite to WIF Hospital, 1st Stage, BTM Layout, Bengaluru, Karnataka 560068
Phone: 070224 51093
Hours: Sunday - Saturday 7AM - 10PM

tech news said...

That is a very good tip especially to those fresh to the blogosphere. Brief but very precise info… Thank you for sharing this one. A must read website article!

Kerrthika K said...

It is a very helpful data. It will help to improve my knowledge about this topic. Thank you for this awesome post.
Corporate Training in Chennai
Corporate Training Companies in Chennai
Soft Skills Training in Chennai
JMeter Training in Chennai
Pega Training in Chennai
Appium Training in Chennai
Advanced Excel Training in Chennai
Oracle Training in Chennai
Social Media Marketing Courses in Chennai
Tableau Training in Chennai
Power BI Training in Chennai

Excelrsolutions said...

I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!. machine learning courses in Bangalore

Priyanka said...

Attend The Course in Data Analytics From ExcelR. Practical Course in Data Analytics Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Course in Data Analytics.
ExcelR Course in Data Analytics
Data Science Interview Questions

dataexpert said...


Very nice job... Thanks for sharing this amazing Machine Learning Courses In Pune and educative blog post!

priyanka said...

data analytics courses

priyanka said...

thanks for sharing,
Machine Learning Courses

offpageseo70 said...

Nice blog,I understood the topic very clearly,And want to study more like this.
Data Scientist Course

dataexpert said...

You completed a number of nice points there. I did a search on the issue and found ExcelR Data Analytics Course Pune nearly all people will have the same opinion with your blog.

Data Science Course said...

Data Science Course Training in Bangalore is the best data science course

priyanka said...

Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.
data science courses in pune

priyanka said...

I am impressed by the information that you have on this blog. It shows how well you understand this subject.
data analytics course

shanjames said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.
Hadoop Training in Hyderabad

shanjames said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.
Hadoop Training in Hyderabad

nikhil reddy said...

Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here

Big Data Analytics Training In Hyderabad
Big Data Analytics Course In Hyderabad

priyanka said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
Data science Interview Questions

Excelr Tuhin said...

keep up the good work. this is an Ossam post. This is to helpful, i have read here all post. i am impressed. thank you. this is our Data Science course in Mumbai
data science course in mumbai | https://www.excelr.com/data-science-course-training-in-mumbai

shanjames said...

The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea.
Data Science Training In Hyderabad

ek said...

I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.
Know more about Data Analytics

Ranvijay Singh said...

SEO Training in Bhopal
Digital Marketing Training in Bhopal
SEO Company in Bhopal
Digital Marketing Company in Bhopal
High PR Profile Creation Sites
Web 2.0 Sites list
Directory Submission Sites list
Article Submission Sites list
Social Bookmarking Sites list
Digital Marketing Course in Bhopal

ravali said...

I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.

data science course

ek said...

I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…
More Info of Machine Learning

Ranvijay Singh said...

SEO Training in Bhopal
Digital Marketing Training in Bhopal
SEO Company in Bhopal
Digital Marketing Company in Bhopal
High PR Profile Creation Sites
Web 2.0 Sites list
Directory Submission Sites list
Article Submission Sites list
Social Bookmarking Sites list
Digital Marketing Course in Bhopal

ravali said...

I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.

data science course

jony blaze said...

Great blog. it’s really helpful. Thank you so much for this incredible guide, . Keep sharing on updated blog posts.
Machine Learning Training in Hyderabad

priyanka said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
data analytics course in Bangalore

priyanka said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
data analytics course in Bangalore

priyanka said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries.
Data Science Course

priyanka said...

wonderful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.
data analytics course in Bangalore

akshaya said...

I take a bow for your extraordinary write-up.It is undeniable that you have to be appreciated for your mindful content.Wondering for your next article. Web Designing Course Training in Chennai | Web Designing Course Training in annanagar | Web Designing Course Training in omr | Web Designing Course Training in porur | Web Designing Course Training in tambaram | Web Designing Course Training in velachery